简体   繁体   中英

Apache Commons Hex Encoding Error

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

eg:

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM