一尘不染

如何在Java中将字节数组转换为十六进制字符串?

java

如何在Java中将字节数组转换为十六进制字符串?


阅读 612

收藏
2020-01-10

共1个答案

一尘不染

可以使用如下代码:

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for (int j = 0; j < bytes.length; j++) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = HEX_ARRAY[v >>> 4];
        hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
    }
    return new String(hexChars);
}
2020-01-10