convert string to hex

Y

yuvalken

Hi,

How do I convert string to hexadecimal?

For example "ab cd" -> 61 62 20 63 64 .

Thanks,

P.
 
T

Thomas Schodt

How do I convert string to hexadecimal?

For example "ab cd" -> 61 62 20 63 64 .

I can make a guess at what you are asking for.

String abcd = "ab cd";
byte[] bytes = abcd.getBytes("US-ASCII");

If you have "exotic" characters in your string
maybe you would rather use

char[] chars = abcd.toCharArray();
 
P

Pete Barrett

Hi,

How do I convert string to hexadecimal?

For example "ab cd" -> 61 62 20 63 64 .
Integer has a toHexString() static method, and it's not difficult to
iterate through a string or array, converting each value as you come
to it. Ther are a couple of minor problems which I'll leave you to
solve for yourself - (i) the integer cast from a char, byte, or short
will be sign extended; and (ii) toHexString doesn't pad with 0s.

Pete Barrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top