3

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
amaseuk
  • 2,147
  • 4
  • 24
  • 43

2 Answers2

7

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

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.

Favonius
  • 13,959
  • 3
  • 55
  • 95