0

I'm trying to convert a String to a byte array which has 2 different bytes. for eg: String s1 = " 055E" I need to convert this into like

byte b1 = Integer.parseInt(05,16);  -  1byte

byte b2 = Integer.parseInt(5E,16);   -  1byte

At the end i need to have a byte array which will have values b1, b2.

byte[] b = {b1, b2};    

Any help on this would be appreciated. thanks in advance

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
SilentCoder
  • 47
  • 1
  • 10
  • 1
    possible duplicate of [Is this the best way to convert String hex to bytes?](http://stackoverflow.com/questions/2648242/is-this-the-best-way-to-convert-string-hex-to-bytes) and http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java – Thilo Jun 27 '12 at 22:59

2 Answers2

2

Try this:

String s1 = " 055E";
s1 = s1.trim();
byte[] b = {
    (byte) Integer.parseInt(s1.substring(0, 2), 16),
    (byte) Integer.parseInt(s1,substring(2), 16)
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks a lot. But the array has 4 different values. I want something like 05 should be converted to an int, 5E should be converted to an int & b1 should have integer equivalent, b2 should have integer equivalent... So as a result array b should have only 2 values. – SilentCoder Jun 27 '12 at 23:21
  • @sarath - I don't understand your comment. The array will have two values, not four. My code does exactly what you describe: it turns `05` into an `int` value and narrows it to a `byte` and then does the same with `5E`. – Ted Hopp Jun 28 '12 at 00:42
  • I think i should loop thru the array to get the int values. I'm just new to java, so dont know much. Because when i try to print out just the array it says [B@fe347 – SilentCoder Jun 28 '12 at 17:15
  • @sarath If you try to just print the array with `System.out.println(b)`, that's what it will print, because arrays don't have an automatic conversion to a readable `String` form. You can loop through the array and print each element, or you can use `System.out.println(Arrays.toString(b))` to get a nicely formatted representation of the array elements. – Ted Hopp Jun 28 '12 at 20:30
-1

First allocate enough memory for the array. Then loop through every pair of characters and convert them to a byte. Store the result in the array.

s = s.trim();
byte[] b = new byte[s.length()/2];

for(int i = 0; i < s.length(); i+= 2) {
    b[i/2] = Byte.parseByte(s.substring(i,i+2),16);
}
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • 2
    That isn't ever going assign a value to `b[1]` (or other odd-index `b` values). It is also going to throw an exception for strings like "05DE" and doesn't deal with the leading space. – Ted Hopp Jun 27 '12 at 23:01
  • Woops, had to run to a meeting. Fixed now. – tskuzzy Jun 27 '12 at 23:17
  • Sorry, still not fixed. `Byte.parseByte` will only parse values up to 0x7F. After that, it throws a `NumberFormatException`. (The problem is that `0x80 > Byte.MAX_VALUE`.) – Ted Hopp Jun 28 '12 at 00:41
  • That's correct though if you're dealing with signed bytes. And on a side note, I'm also from Gaithersburg :-o Small world. – tskuzzy Jun 28 '12 at 00:48
  • How about that! My older son was also in the math/sci magnet at Blair (a few years ahead of you, though). Very small world. :) – Ted Hopp Jun 28 '12 at 01:32