Compare a byte and hexadecimal

Exception

I have a byte array which I need to traverse and based on the byte value do something. Below is my code for this.

    for (byte b:byteArray)
    {

        if(b>=(byte)0x00 && b<=(byte)0x1F)
        {
            //do something
        }
        else if(b>=(byte)0x20 && b <=(byte)0xFE)
        {   
             //do something
        }
        else if(b ==(byte)0xFF)
        {
            //do something
        }
    }

My problem the byte comparison is not happening as per my expectation.

Assume the values in my byte array is {31, -117, 8, 0 , -1}, the code I have is not able to detect all negative byte values in their respective range. For ex. -117 should be between 0x20 and 0xFE , but it is not going to any of the range.

I have followed this answer to compare bytes. I cannot figure out what is wrong in the code.

Thanks in advance!

Fildor

Your relations only make sense on unsigned bytes - which do not exist in Java.

Try this:

for (byte b:byteArray)
{
    int ib = b & 0xFF;         // This scales up to integer so you have values 0-255
    if(ib >=0x00 && ib<=0x1F)
    {
        //do something
    }
    else if(ib>=0x20 && ib <=0xFE)
    {   
         //do something
    }
    else if(ib == 0xFF)
    {
        //do something
    }
    else
    {
        // log a bug! This should never happen!
    }
}

In your solution -117 is 0x8B , which is in fact less than 0x20 (Dec 32), so it does not meet the 2nd path criteria b >= 0x20. In fact this creates unreachable code because no value is > 32 and <= -2.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Comparison of byte value 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

Compare two files Byte by Byte

From Dev

Compare 2 byte arrays

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

Interpret a byte array literal as hexadecimal byte array literal

From Dev

Python compare integer number with hexadecimal and binary numbers

From Dev

How to compare [32]byte with []byte in golang?

From Dev

Compare 16 byte strings with SSE

From Java

How to convert a text file having a hexadecimal code sequence into a byte array?

From Java

How do you convert a byte array to a hexadecimal string, and vice versa?

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

byte.Parse (c#) : FormatException on Hexadecimal string

From Dev

How to loop through a byte array in C programming and store in a string as Hexadecimal

From Dev

How do I save a decimal number as hexadecimal on a single byte?

From Dev

How to efficiently compare a block of memory to a single byte?

From Dev

Compare string and byte slice in Go without copy

From Dev

Fast way to compare 2 byte arrays

From Dev

Compare String with String (Byte array from DatagramPacket)

Related Related

  1. 1

    Comparison of byte value and hexadecimal

  2. 2

    Java: Convert a hexadecimal encoded String to a hexadecimal byte

  3. 3

    Converting a hexadecimal String to byte array

  4. 4

    converting hexadecimal string to byte array

  5. 5

    Adding one byte to a hexadecimal number

  6. 6

    Converting a hexadecimal String to byte array

  7. 7

    Java: Simply convert a decimal byte to a hexadecimal byte

  8. 8

    Compare two files Byte by Byte

  9. 9

    Compare 2 byte arrays

  10. 10

    How to create byte[] from hexadecimal values?

  11. 11

    Translating an integer into two-byte hexadecimal with Python

  12. 12

    Add hexadecimal from file to List<byte>

  13. 13

    Hexadecimal: Set an 8-bit byte in an int

  14. 14

    How to convert byte to hexadecimal string in Java

  15. 15

    Interpret a byte array literal as hexadecimal byte array literal

  16. 16

    Python compare integer number with hexadecimal and binary numbers

  17. 17

    How to compare [32]byte with []byte in golang?

  18. 18

    Compare 16 byte strings with SSE

  19. 19

    How to convert a text file having a hexadecimal code sequence into a byte array?

  20. 20

    How do you convert a byte array to a hexadecimal string, and vice versa?

  21. 21

    Can we read 4th byte from hexadecimal value?

  22. 22

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

  23. 23

    byte.Parse (c#) : FormatException on Hexadecimal string

  24. 24

    How to loop through a byte array in C programming and store in a string as Hexadecimal

  25. 25

    How do I save a decimal number as hexadecimal on a single byte?

  26. 26

    How to efficiently compare a block of memory to a single byte?

  27. 27

    Compare string and byte slice in Go without copy

  28. 28

    Fast way to compare 2 byte arrays

  29. 29

    Compare String with String (Byte array from DatagramPacket)

HotTag

Archive