在Java中,如何将十六进制字符串转换为byte []? [重复]

时间:2021-10-07 08:27:52

This question already has an answer here:

这个问题在这里已有答案:

I am using the below function in Java to convert an encrypted String into hex format:

我在Java中使用以下函数将加密的String转换为十六进制格式:

public static String toHex(byte [] buf) {
    StringBuffer strbuf = new StringBuffer(buf.length * 2);
    int i;
    for (i = 0; i < buf.length; i++) {
        if (((int) buf[i] & 0xff) < 0x10) {
            strbuf.append("0");
        }
        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }
    return strbuf.toString();
}

Now I want to convert that hex string back into a byte array. How can I do that?

现在我想将该十六进制字符串转换回字节数组。我怎样才能做到这一点?

For example,

(1) Plain Text = 123
(2) Encrypted Text = «h>kq*«¬Mí“~èåZ  \}?
(3) Encrypted Text in Hex = f263575e7b00a977a8e9a37e08b9c215feb9bfb2f992b2b8f11e

I can go from (2) to (3), but how do I go from (3) back to (2)?

我可以从(2)到(3),但我如何从(3)回到(2)?

3 个解决方案

#1


44  

 String s="f263575e7b00a977a8e9a37e08b9c215feb9bfb2f992b2b8f11e";
 byte[] b = new BigInteger(s,16).toByteArray();

#2


91  

The accepted answer does not consider leading zeros which may cause problems

接受的答案不会考虑可能导致问题的前导零

This question answers it. Depending on whether you want to see how its done or just use a java built-in method. Here are the solutions copied from this and this answers respectively from the SO question mentioned.

这个问题回答了它。取决于您是想要查看其完成方式还是仅使用java内置方法。以下是从这里复制的解决方案,这个答案分别来自提到的SO问题。

Option 1: Util method

选项1:Util方法

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

Option 2: One-liner build-in

选项2:单线内置

import javax.xml.bind.DatatypeConverter;

public static String toHexString(byte[] array) {
    return DatatypeConverter.printHexBinary(array);
}

public static byte[] toByteArray(String s) {
    return DatatypeConverter.parseHexBinary(s);
}

#3


6  

I found that the DatatypeConverter.parseHexBinary is more costly (two times) than:

我发现DatatypeConverter.parseHexBinary比以下成本更高(两倍):

org.apache.commons.codec.binary.Hex(str.toCharArray())

#1


44  

 String s="f263575e7b00a977a8e9a37e08b9c215feb9bfb2f992b2b8f11e";
 byte[] b = new BigInteger(s,16).toByteArray();

#2


91  

The accepted answer does not consider leading zeros which may cause problems

接受的答案不会考虑可能导致问题的前导零

This question answers it. Depending on whether you want to see how its done or just use a java built-in method. Here are the solutions copied from this and this answers respectively from the SO question mentioned.

这个问题回答了它。取决于您是想要查看其完成方式还是仅使用java内置方法。以下是从这里复制的解决方案,这个答案分别来自提到的SO问题。

Option 1: Util method

选项1:Util方法

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

Option 2: One-liner build-in

选项2:单线内置

import javax.xml.bind.DatatypeConverter;

public static String toHexString(byte[] array) {
    return DatatypeConverter.printHexBinary(array);
}

public static byte[] toByteArray(String s) {
    return DatatypeConverter.parseHexBinary(s);
}

#3


6  

I found that the DatatypeConverter.parseHexBinary is more costly (two times) than:

我发现DatatypeConverter.parseHexBinary比以下成本更高(两倍):

org.apache.commons.codec.binary.Hex(str.toCharArray())