Java 的16 进制与字符串的相互转换函数 1. /** 2. * 将指定 byte 数组以 16 进制的形式打印到控制台 3. * @param hint String 4. * @param b byte[] 5. * @return void 6. */ 7. public static void printHexString(String hint, byte[] b) { 8. System.out.print(hint); 9. for (int i = 0; i < b.length; i++) { 10. String hex = Integer.toHexString(b[i] & 0xFF); 11. if (hex.length() == 1) { 12. hex = '0' + hex; 13. } 14. System.out.print(hex.toUpperCase() + " "); 15. } 16. System.out.println(""); 17. } 1. /** 2. * 3. * @param b byte[] 4. * @return String 5. */ 6. public static String Bytes2HexString(byte[] b) { 7. String ret = ""; 8. for (int i = 0; i < b.length; i++) { 9. String hex = Integer.toHexString(b[i] & 0xFF); 10. if (hex.length() == 1) { 11. hex = '0' + hex; 12. } 13. ret += hex.toUpperCase(); 14. } 15. return ret; 16. } 1. /** 2. * 将两个ASCII 字符合成一个字节; 3. * 如:"EF"--> 0xEF 4. * @param src0 byte 5. * @param src1 byte 6. * @return byte 7. */ 8. public static byte uniteBytes(byte src0, byte src1) { 9. byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); 10. _b0 = (byte)(_b0 << 4); 11. byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); 12. byte ret = (byte)(_b0 ^ _b1); 13. return ret; 14. } 1. /** 2. * 将指定字符串 src,以每两个字符分割转换为 16 进制形式 3. * 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 4. * @param src String 5. * @return byte[] 6. */ 7. public static byte[] HexString2Bytes(String src){ 8. byte[] ret = new byte[8]; 9. byte[] tmp = src.getBytes(); 10. for(int i=0; i<8; i++){ 11. ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); 12. } 13. return ret; 14. } CRC16Util package com.sunwei.sim4xian; import sun.misc.CRC16; public clas...