3

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!

1
  • Treat it as an unsigned byte and comparison is the same. Commented Dec 8, 2011 at 14:53

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

Comments

3

You can promote the byte to integer:

if((byteArray[13] & 0xff) == 0xec)

Comments

0

since java doesn't support (atleast with primitives) any unsigned data types, you should look at using int as your data type to parse the string..

        String str = "a55a0b05000000000022366420ec";
        int[] arrayOfValues = new int[str.length() / 2];
        int counter = 0;
        for (int i = 0; i < str.length(); i += 2) {
            String s = str.substring(i, i + 2);
            arrayOfValues[counter] = Integer.parseInt(s, 16);
            counter++;
        }

work with the arrayOfValues...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.