3

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 } ?

Community
  • 1
  • 1
artem
  • 16,382
  • 34
  • 113
  • 189
  • 2
    See [Convert a string representation of a hex dump to a byte array using Java?](http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java) – wjans Jun 28 '11 at 12:48
  • 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
  • @Nick is right, take a look at the array he wants produced. – alexcoco Jun 28 '11 at 12:56
  • @alexcoco - then the wording of his question is very misleading – Richard H Jun 28 '11 at 12:59
  • @Richard I don't disagree with you there. It's not clear at all. – alexcoco Jun 28 '11 at 13:01
  • 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
  • @Richard H - sorry, but it is very clear - he wants to parse a string that contains bytes in hex notation. And it is clearly a duplicate (wjans found the very same question). – Andreas Dolk Jun 28 '11 at 13:06
  • @Richard That's his decision. I haven't voted on any answers whatsoever. None that I saw seem to answer what I think RankoR is asking but the question is not clear so I suppose it's no one's fault that they're technically wrong. I just wanted to point out that most people didn't understand the question. At least, I think they didn't understand. We'll have to wait for an accepted answer to be sure. – alexcoco Jun 28 '11 at 13:07
  • Just fix those answers already -- getBytes() is clearly not the right answer – Nick Jun 28 '11 at 13:14
  • To everyone commenting here (other than Nick and wjans): I think from the question (and the example) it is totally clear what RankoR wants - convert a hexadecimal representation to a byte array. **Please read the whole question, not only the title.** – Paŭlo Ebermann Jun 28 '11 at 13:33

4 Answers4

6

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.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • 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
  • @Paŭlo Ebermann: Thanks for that, I've edited my answer accordingly. – tskuzzy Jun 28 '11 at 13:31
3

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)

Community
  • 1
  • 1
Charlie
  • 7,181
  • 1
  • 35
  • 49
2

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;
Thomas
  • 87,414
  • 12
  • 119
  • 157
1

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.

musiKk
  • 14,751
  • 4
  • 55
  • 82