1

I'm trying to convert specific String to a byte[].

The String looks like: 55 55 F5 FF FF

and here is the method I wrote for it, hope someone can tell me what is wrong or has a better solution.

public static byte[] stringToByte(String text) {
    //byte[] length ==> String (length+1)/3
    byte raw[] = new byte[(text.length() + 1) / 3];
    int j = 0;
    for (int i = 0; i < text.length(); i += 2) {
        String h = "";
        h += text.charAt(i);
        h += text.charAt(i + 1);
        i++;
        raw[j] = Byte.valueOf(h);
        j++;
    }
    return raw;
}

The problem is it works until it comes to F5.

I need the same values in the byte[] as if I use

byte raw[] = {(byte) 0x55, (byte) 0x55, (byte) 0x5F,(byte) 0xFF,(byte) 0xFF};
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Jay
  • 281
  • 1
  • 4
  • 13
  • No sorry its the false on. When I use "raw=text.getBytes();" i get a array with 14 positions but i need one with 5 like the last codeblock in my question. – Jay May 09 '12 at 21:17
  • If you have a look at the documentation for `Byte.valueOF`, it will point you to `parseByte` which says "The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign". That is why it blows up when you give it an `F`. – James Montagne May 09 '12 at 21:18
  • 2
    Oh, now I see - I think this one fits: http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java – wkl May 09 '12 at 21:18

4 Answers4

2

This will work:

public static byte[] stringToByte(String text) {
  final String[] split = text.split("\\s+");
  final byte[] result = new byte[split.length];
  int i = 0;
  for (String b : split) result[i++] = (byte)Integer.parseInt(b, 16);
  return result;
}

Why? byte is a signed quantity and you cannot parse anything out of its range directly into byte. But if you parse an int and then use a narrowing conversion into byte, you'll get your result.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

It looks like you're just looping through a space delimited String and parsing out Byte objects. In the case, I'd suggest using something like:

         String text = "";
         String[] strArr = text.split(" ");
         for (String str: strArr)
         {
             //convert string to byte here
         }
Jon7
  • 7,165
  • 2
  • 33
  • 39
0
byte[] raw = new byte[text.length() >> 1];
for(int i=0; i<text.length();i+=2){
    raw[i >> 1] = Integer.parseInt(text.substring(i, i+2),16);
}

I used Integer.parseInt() instead of Byte.parseByte() because Byte is not unsigned byte. I tried it with String text = "55555FFF";.

user845279
  • 2,794
  • 1
  • 20
  • 38
0

You could also use StringTokenizer for each token.

public static byte[] stringToByte(String text) {
    StringTokenizer st = new StringTokenizer(text);
    byte raw[] = new byte[st.countTokens()];
    for (int i = 0; i < raw.length; i++) {
        raw[i] = (byte) Integer.parseInt(st.nextToken(), 16);
    }
    return raw;
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148