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