Possible Duplicate:
Convert a string representation of a hex dump to a byte array using Java?

For example, I have a string "DEADBEEF". How can I convert it to byte[] bytes = { 0xDE, 0xAD, 0xBE, 0xEF } ?

share|improve this question

76% accept rate
2  
3  
Why don't people use the API? :[ – mre Jun 28 '11 at 12:49
1  
Are you trying to convert the string to hex, or to ASCII bytes? The ASCII bytes for "DEADBEEF" are not { 0xDE, 0xAD, 0xBE, 0xEF }. – aroth Jun 28 '11 at 12:50
2  
He did not say ASCII -- why is everyone assuming he wants ASCII? That is what you get for answering a question within 3 seconds of reading it. – Nick Jun 28 '11 at 12:52
1  
@alexcoco - given that no-one actually knows what this guy is asking and the OP hasn't clarified, it's pretty harsh for Nick to dv everyone. – Richard H Jun 28 '11 at 13:02
show 8 more comments
feedback

closed as exact duplicate by Andreas_D, Paŭlo Ebermann, Richard H, Kev Jun 28 '11 at 13:29

This question covers exactly the same content as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

5 Answers

up vote 6 down vote accepted

Loop through each pair of two characters and convert each pair individually:

byte[] bytes = new byte[str.length()/2];

for( int i = 0; i < str.length(); i+=2 )
    bytes[i/2] = ((byte)Character.digit(str.charAt(i),16))<<4)+(byte)Character.digit(str.charAt(i),16);

I haven't tested this code out (I don't have a compiler with me atm) but I hope I got the idea through. The subtraction/addition simply converts 'A' into the number 10, 'B' into 11, etc. The bitshifting <<4 moves the first hex digit to the correct place.

EDIT: After rethinking it a bit, I'm not sure if you're asking the correct question. Do you want to convert "DE" into {0xDE}, or perhaps into {0x44,0x45} ? The latter is more useful, the former is more like a homework problem type question.

share|improve this answer
1  
+ 1Yeah -- somebody who got the question! – Nick Jun 28 '11 at 12:54
Why would the latter be more useful? The first is standard hex encoding. I'm hard pressed to find a problem that fits your "better" interpretation. – musiKk Jun 28 '11 at 13:11
@musiKk: The latter is called "ASCII encoding", not "hexadecimal decoding". – Paŭlo Ebermann Jun 28 '11 at 13:22
1  
@tskuzzy: -1. This code is specialized to the input - it works only with the hexadecimal digits A to F, not with normal 0 to 9 (which also occur in hexadecimal numbers). Better use Character.digit(c, 16) instead of your c - 'A' + 10. (Please comment after you fix this, and I will undo my downvote.) – Paŭlo Ebermann Jun 28 '11 at 13:26
@musiKk: Oh never mind, you're right. I was thinking more along the lines of an arbitrary String (e.g. "QWERTY") being converted in that fashion (which obviously makes no sense). – tskuzzy Jun 28 '11 at 13:30
show 1 more comment
feedback

getBytes() would get you the bytes of the characters in the platform encoding. However it sounds like you want to convert a String containing a Hex representation of bytes into the actual represented byte array.

In which case I would point you toward this existing question: Convert a string representation of a hex dump to a byte array using Java? (note: I personally prefer the 2nd answer to use commons-codec but more out of philosophical reasons)

share|improve this answer
feedback

You can parse the string to a long and then extract the bytes:

String s = "DEADBEEF";
long n = Long.decode( "0x" + s );  //note the use of auto(un)boxing here, for Java 1.4 or below, use Long.decode( "0x" + s ).longValue();
byte[] b = new byte[4];
b[0] = (byte)(n >> 24);
b[1] = (byte)(n >> 16);
b[2] = (byte)(n >> 8);
b[3] = (byte)n;
share|improve this answer
This works in this case, but in general only for strings up to 16 hex digits (i.e. 8 decoded bytes) long. – Paŭlo Ebermann Jun 28 '11 at 13:28
@Paulo that's true, but you could split the string into batches of 16 characters if needed. – Thomas Jun 28 '11 at 13:54
feedback

tskuzzy's answer might be right (didn't test) but if you can, I'd recommend using Commons Codec from Apache. It has a Hex class that does what you need.

share|improve this answer
feedback
String s = "DEADBEEF";

byte[] bytes = s.getBytes();

See the docs.

Edit: for the dv'ers: I have apparently answered the wrong question. Nevertheless it is correct for the question title, so I am leaving as is.

share|improve this answer
1  
How exactly is this useful? The guy wants to convert "DE" to {0xDE} -- what CharSet mapping will do that? – Nick Jun 28 '11 at 12:50
@Nick - maybe I have misunderstood the question. He wanted bytes. My answer is identical to the top voted comment. Is he wrong too? – Richard H Jun 28 '11 at 12:52
Very much so :) Have downvoted everyone, I hope I do not run out of rep points :) – Nick Jun 28 '11 at 12:53
@Nick, Sometimes a simple comment will suffice. There's really no need to just down-vote everyone. – mre Jun 28 '11 at 13:06
Just correct your answers and I will undo my downvote -- otherwise, it needs to be flagged. imagine the frustration of someone googling an answer to this very problem – Nick Jun 28 '11 at 13:09
show 4 more comments
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.