读书人

数目字类型转换

发布时间: 2012-09-28 00:03:35 作者: rapoo

数字类型转换

在对APDU进行十进制、十六进制、字节、字符串转换时用到下列方法:

package com.example.android.BluetoothChat;public class Convertor {    /* 给APDU添加前缀和后缀 */    public static String m_encode_apdu(String apdu)    {        int len = apdu.length() / 2;        String lenHex = m_convert_int_to_hex_string(len);        return "02" + "00" + lenHex + "80" + "FFFFFFFF" +            apdu + "01" + "03";    }        /* 十进制数值转换为十六进制字符串 */    public static String m_convert_int_to_hex_string(int d){    String hex = Integer.toHexString(d);    if (hex.length() == 1) {               hex = '0' + hex;           }    return hex.toUpperCase();    }        /* 字节数值转换为十六进制字符串 */    public static String m_convert_byte_to_hex_string(byte b){    String hex = Integer.toHexString(b & 0xFF);           if (hex.length() == 1) {               hex = '0' + hex;           }        return hex.toUpperCase();    }        /* 字节数组转换为十六进制字符串 */public static String m_convert_byte_array_to_hex_string(byte[] ba) {String hex = "";for (int i = 0; i < ba.length; i++) {hex += m_convert_byte_to_hex_string(ba[i]);}return hex;}        /* 整形数值转换为长度为4的字节数组 */    public static byte[] m_convert_int_to_byte_array(int d)    {        byte[] ba = new byte[4];        for (int i = 0; i < 4; i++)        {            ba[i] = (byte)(d >> 8 * (3 - i) & 0xFF);        }        return ba;    }        /* 整形数值转换为十六进制字符串 */    public static String m_convert_int_to_byte_hex_String(int d){    byte[] ba = m_convert_int_to_byte_array(d);    String hexString = m_convert_byte_array_to_hex_string(ba);    return hexString;    }}

?

读书人网 >移动开发

热点推荐