Hi,
How do I convert string to hexadecimal?
For example "ab cd" -> 61 62 20 63 64 .
Thanks,
P.
Thomas Schodt - 16 May 2005 12:52 GMT
> 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();
Pete Barrett - 16 May 2005 19:32 GMT
>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