Converting a hexadecimal string representation of some bytes into a byte array in Java

It's hard for me to try to convert a string containing the hexadecimal representation of some bytes into the corresponding byte array.

I get 32 ​​bytes using the following code:

StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
    sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();

Any idea how to go from String to array? In other words, how to do the reverse code above.

Thank.

+1
source share
3 answers

I would try commons-codec byte[] originalBytes = Hex.decodeHex(string.toCharArray()). In fact, I would use it for the coding part as well.

+4
source

Use

String.getBytes();

Method

-1
source

All Articles