Comparison of byte value and hexadecimal

Barracuda

I ran into a problem when comparing the value of a byte. Theoretically, the following code snippet should return true, yet it returns false.

public static void main(String [] args) {
    byte a = 3;
    if (MpegUtil.isSlice(a) == true) {
        System.out.print("Yes");
    }
    else System.out.print("No");
}

public static boolean isSlice(byte ID) {
    if (ID >= 1 && ID <= (byte) 0xaf) return true;
    else return false;
}

What the function isSlice does is: receive a byte and if it is between 1 and af (hex), it will return true, if it doesn't the returned value is False. Yet somehow it fails my test by answering false to 3.

Does anyone has an idea of why this function doesn't work?

EDIT: Thank-you for the suggestion of removing the 'byte' cast, it does indeed allow the code to function, however once the compared value is superior or equal to '0x80' the result change back to being a wrong 'false'. I know this is caused by the fact that Java considers the byte to be signed (now i'm really looking forward to Java 8), but would there be any "cleaner" version than changing the condition to:

if (ID != 0 && ID >= (byte) 0xaf && ID <= (byte) 0x7f) return true

Since this condition doesn't seem at first glance logical, is there any more obvious way to fix this?

Christian

It doesn't work because (byte) 0xAF is not what you think. Try printing the following, so you get the idea:

    System.out.println(0xaf); // 175
    System.out.println((byte) 0xaf); // -81

Why is (byte) 0xAF negative?

Print this:

System.out.println(Byte.MAX_VALUE); // 127

The range of a byte is [-128, 127] (256 numbers), so if you cast 175 to byte, you will get:

175 - 256 = -81

You can also try this:

System.out.println((byte) 128); // -128
System.out.println((byte) -129); // 127
System.out.println((byte) 256); // 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can we read 4th byte from hexadecimal value?

From Dev

Converting byte array to hexadecimal value using BitConverter class in c#?

From Dev

Hexadecimal number comparison?

From Dev

Compare a byte and hexadecimal

From Dev

Java: Convert a hexadecimal encoded String to a hexadecimal byte

From Dev

Converting a hexadecimal String to byte array

From Dev

converting hexadecimal string to byte array

From Dev

Adding one byte to a hexadecimal number

From Dev

Converting a hexadecimal String to byte array

From Dev

Java: Simply convert a decimal byte to a hexadecimal byte

From Dev

Python Comparison of byte literals

From Dev

byte comparison vs hexstring

From Dev

converting decimal to hexadecimal value

From Dev

scanf a big hexadecimal value

From Dev

ObjectID not storing hexadecimal value

From Dev

How to create byte[] from hexadecimal values?

From Dev

Translating an integer into two-byte hexadecimal with Python

From Dev

Add hexadecimal from file to List<byte>

From Dev

Hexadecimal: Set an 8-bit byte in an int

From Dev

How to convert byte to hexadecimal string in Java

From Dev

Use a Decimal Value as a Hexadecimal Value

From Dev

Use a Decimal Value as a Hexadecimal Value

From Dev

Interpret a byte array literal as hexadecimal byte array literal

From Dev

Purpose of hexadecimal floating point value?

From Dev

Corebluetooth write Hexadecimal Value for Characteristic

From Dev

Converting textbox value to hexadecimal? Javascript

From Dev

Converting RGB/Brush to Hexadecimal value

From Dev

Latitude and longitude from hexadecimal value

From Dev

Purpose of hexadecimal floating point value?