javahexapache-commons-codec

Apache Commons Hex Encoding Error


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?


Solution

  • 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.)