Reputation: 135
I got a stream of byte this stream is a bytearray ( byte[] stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int
But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"
How could i manage this exception? Maybe with an other type of parse or different type data?
byte k = stream[i];
int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
Upvotes: 0
Views: 541
Reputation: 614
You must use overloaded parseInt method with radix. For hex value an example:
Integer.parseInt("84B",16);
Upvotes: 3
Reputation: 135
I've resolved my problem with this question
byte k = stream[i];
int b = 0;
try {
String hexStringNumber = String.format("0x%02X",k);
b = Integer.decode(hexStringNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 842
Try this:
Integer.parseInt(String.format("0b%02X",k),16);
Binary values are written using "0bxx" syntax
Upvotes: 0