How to convert hex string to Java string?

Samra picture Samra · Dec 21, 2012 · Viewed 148.7k times · Source

For logging purpose we are converting the logs to byte array and then to hex string. I want to get it back in a Java String, but I am not able to do so.

The hex string in log file looks something like

fd00000aa8660b5b010006acdc0100000101000100010000

How can I decode this?

Answer

Reimeus picture Reimeus · Dec 21, 2012

Using Hex in Apache Commons:

String hexString = "fd00000aa8660b5b010006acdc0100000101000100010000";    
byte[] bytes = Hex.decodeHex(hexString.toCharArray());
System.out.println(new String(bytes, "UTF-8"));