RT:byte字节数组怎么转换成16进制!

解决方案 »

  1.   

    public class Test {    public static void main(String args[]) {        
            String str = "Hello";
            byte[] b = str.getBytes();
            System.out.println(byte2HexString(b));
        }
        
        public static String byte2HexString(byte[] b) {
            char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
                          '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            char[] newChar = new char[b.length * 2];
            for(int i = 0; i < b.length; i++) {
                newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
                newChar[2 * i + 1] = hex[b[i] & 0xf];
            }
            return new String(newChar);
        }
    }
      

  2.   

    hello 的 5 个 byte 是:0x48 0x65 0x6C 0x6C 0x6F以第一个字节:0x6C 为例:(b[i] & 0xf0) >> 4
    (0x6C & 0xf0) >> 4 可以获得高 4 位,即 0x6    01101100  0x6C
    &  11110000  0xF0
    ------------------
        01100000  0x60再将 01100000 左移 4 位就得到了 0110 即 0x6hex[0x6] 取数组中的索引为 6 的字符,即“6”b[i] & 0xf
    0x6C & 0xf 可以获得低 4 位,即 0xC
    hex[0xC] 取数组中的索引为 12 的字符,即“C”这样就完成一个字节的转换。
      

  3.   

    如果用 JDK 5 以上的版本,直接用:String byteStr = String.format("%02X", b[i]);就可以完成了。
      

  4.   

    请问 反过来 把16进制怎么转换成byte字节数组?
      

  5.   

    public class Test {    public static void main(String args[]) {        
            String str = "Hello";
            byte[] b = str.getBytes();
            String s = byte2HexString(b);
            System.out.println(s);
            System.out.println(new String(hexString2ByteArray(s)));
            
        }
        
        public static String byte2HexString(byte[] b) {
            char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
                          '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            char[] newChar = new char[b.length * 2];
            for(int i = 0; i < b.length; i++) {
                newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
                newChar[2 * i + 1] = hex[b[i] & 0xf];
            }
            return new String(newChar);
        }
        
        public static byte[] hexString2ByteArray(String hexString) {
            if(hexString.length() % 2 != 0) {
                throw new IllegalArgumentException("error");
            }
            char[] chars = hexString.toCharArray();
            byte[] b = new byte[chars.length / 2];
            for(int i = 0; i < b.length; i++) {
                int high = Character.digit(chars[2 * i], 16) << 4;
                int low = Character.digit(chars[2 * i + 1], 16);
                b[i] = (byte)(high | low);
            }
            return b;
        }
    }
      

  6.   


    public static String Bytes2HexString(byte[] b)    //byte转换为十六进制
    {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
          String hex = Integer.toHexString(b[i]& 0xFF);
          if (hex.length() == 1) {
            hex = '0' + hex;
          }
          ret += hex.toUpperCase();
        }
        return ret;
    }
      

  7.   


    public static byte uniteBytes(byte src0, byte src1)
    {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
        _b0 = (byte)(_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
        byte ret = (byte)(_b0 | _b1);
        return ret;
    }
      
    public static byte[] HexString2Bytes(String src)    //十六进制转byte
    {
       
        byte[] tmp = src.getBytes();
        byte[] ret = new byte[tmp.length/2];
        for(int i=0; i<ret.length; i++){
          ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
        }
        return ret;
      }

    public static String byte2HexString(byte[] b) {
            char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
                          '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            char[] newChar = new char[b.length * 2];
            for(int i = 0; i < b.length; i++) {
                newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
                newChar[2 * i + 1] = hex[b[i] & 0xf];
            }
            return new String(newChar);
        }这是我写的,大家给提一点建议,谢谢
      

  8.   

    注:byte2HexString//这个不是我写的,