Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Hex String to unsigned Byte Array in Java

Tags:

java

hex

I get hex strings of 14 bytes, e.g. a55a0b05000000000022366420ec. I use javax.xml.bind.DatatypeConverter.parseHexBinary(String s) to get an array of 14 bytes. Unfortunately those are unsigend bytes like the last one 0xEC = 236 for example.

But I would like to compare them to bytes like this: if(byteArray[13] == 0xec) Since 235 is bigger than a signed byte this if statement would fail. Any idea how to solve this in java? Thx!

like image 671
tzippy Avatar asked May 06 '26 12:05

tzippy


2 Answers

Try if(byteArray[13] == (byte)0xec)

like image 194
korifey Avatar answered May 09 '26 03:05

korifey


You can promote the byte to integer:

if((byteArray[13] & 0xff) == 0xec)
like image 36
MByD Avatar answered May 09 '26 04:05

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!