Apache Commons Hex Encoding Error

3.4k views Asked by At

I'm trying to use org.apache.commons.codec.binary.Hex to encode and decode a String value:

e.g.:

Hex.encodeHex("10".getBytes()).toString();

However, this is not giving me a hexadecimal output, but outputs similar to this:

[C@596d444a

Any ideas why this is happening?

2

There are 2 answers

0
Jon Skeet On BEST ANSWER

Yes - the call to encodeHex() returns a char array (char[]) and you're just calling toString on that. Use the String(char[]) constructor instead:

new String(Hex.encodeHex("10".getBytes()))

(I would strongly encourage you not to use the parameterless String.getBytes() method, by the way, which uses the platform default encoding. It's a constant source of subtle errors.)

0
Favonius On

As per the link you have given: public static char[] encodeHex(byte[] data) return @return A char[] containing hexadecimal characters. Hence the output is right. Create a string using the char array.