如何将六进制字符串转换为二进制字节数组?

时间:2019-03-29 15:40:06

标签: java binary hex byte

我想将六进制字符串转换为标题中所写的二进制字节数组

主要代码:

String r16 = "15d626926b7567f7";

byte[] binary64 = hexToBinary64Bits(r16);
System.out.println("r16 : " + r16);

for (int i = 0; i < binary64.length; i++) {
    System.out.print(binary64[i] + ", ");
}

   public static byte[] hexToBinary64Bits(String hex) {

    return ((String.format("%64s", 

        Long.toBinaryString(Long.parseLong(hex,16))).

        replace(" ", "0")).getBytes());

}

结果

r16 : 15d626926b7567f7

48, 48, 48, 49, 48, 49, 48, 49, 49, 49, 48, 49, 48, 49, 49, 48, 48, 48, 49, 48, 48, 49, 49, 48, 49, 48, 48, 49, 48, 48, 49, 48, 48, 49, 49, 48, 49, 48, 49, 49, 48, 49, 49, 49, 48, 49, 48, 49, 48, 49, 49, 48, 48, 49, 49, 49, 49, 49, 49, 49, 48, 49, 49, 49,

我不想拥有48、48、48、49,但是拥有0、0、0、1,我必须是效率最高的:)

非常感谢!

0 个答案:

没有答案