How to convert a byte array to a hex string in Java?

后端 未结 27 3471
花落未央
花落未央 2020-11-21 04:19

I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in

27条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 04:48

    Converts bytes data to hex characters
    
    @param bytes byte array to be converted to hex string
    @return byte String in hex format
    
    private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }
    

提交回复
热议问题