55

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?

Kyll
  • 7,036
  • 7
  • 41
  • 64
Samra
  • 875
  • 1
  • 9
  • 15

7 Answers7

74

Using Hex in Apache Commons:

String hexString = "fd00000aa8660b5b010006acdc0100000101000100010000";    
byte[] bytes = Hex.decodeHex(hexString.toCharArray());
System.out.println(new String(bytes, "UTF-8"));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
34
byte[] bytes = javax.xml.bind.DatatypeConverter.parseHexBinary(hexString);
String result= new String(bytes, encoding);
telebog
  • 1,706
  • 5
  • 25
  • 34
13

You can go from String (hex) to byte array to String as UTF-8(?). Make sure your hex string does not have leading spaces and stuff.

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

Usage:

String b = "0xfd00000aa8660b5b010006acdc0100000101000100010000";
byte[] bytes = hexStringToByteArray(b);
String st = new String(bytes, StandardCharsets.UTF_8);
System.out.println(st);
catch23
  • 17,519
  • 42
  • 144
  • 217
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
10

First of all read in the data, then convert it to byte array:

 byte b = Byte.parseByte(str, 16); 

and then use String constructor:

new String(byte[] bytes) 

or if the charset is not system default then:

new String(byte[] bytes, String charsetName) 
Aleksander Gralak
  • 1,509
  • 10
  • 26
6

Try the following code:

public static byte[] decode(String hex){

        String[] list=hex.split("(?<=\\G.{2})");
        ByteBuffer buffer= ByteBuffer.allocate(list.length);
        System.out.println(list.length);
        for(String str: list)
            buffer.put(Byte.parseByte(str,16));

        return buffer.array();

}

To convert to String just create a new String with the byte[] returned by the decode method.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
5

Just another way to convert hex string to java string:

public static String unHex(String arg) {        

    String str = "";
    for(int i=0;i<arg.length();i+=2)
    {
        String s = arg.substring(i, (i + 2));
        int decimal = Integer.parseInt(s, 16);
        str = str + (char) decimal;
    }       
    return str;
}
Kunal Surana
  • 659
  • 5
  • 14
5

There is yet another way to do this if using JDK17 or above, see the new HexFormat class which has useful methods for converting byte[] <=> hexadecimal String :

String hexStr ="5468697320697320616e206578616d706c6520737472696e67";
byte[] bytes = HexFormat.of().parseHex(hexStr);
String str = new String(bytes, StandardCharsets.UTF_8 /* or Charset.forName("whateverencoding")*/);

You of course need to know the exact character encoding of the hexadecimal representation in order to generate the correct String value.

DuncG
  • 12,137
  • 2
  • 21
  • 33