Java – Converting A String To Hexadecimal In Java

hexjavastring

I am trying to convert a string like "testing123" into hexadecimal form in java. I am currently using BlueJ.

And to convert it back, is it the same thing except backward?

Best Answer

Here's a short way to convert it to hex:

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}