Converting A String To Hexadecimal In Java

Keith picture Keith · May 29, 2009 · Viewed 425.5k times · Source

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?

Answer

Kaleb Pederson picture Kaleb Pederson · Jan 27, 2010

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?*/)));
}