How to check byte for hexadecimal value?

I want to check if the byte that I read from the data file is 0xEE, what should I do? I tried if (aChar == 0xEE)but it doesn’t work.

+3
source share
2 answers

When reading a signed char, the value will never be 0xEE. IE:

#include <stdio.h>

int main()
{
    char c = 0xEE;   // assumes your implementation defines char as signed char
    // c is now -18
    if (c == 0xEE)
    {
        printf("Char c is 0xEE");
    }
    else
    {
        printf("Char c is NOT 0xEE");
    }

}

The output will be

Char c is NOT 0xEE

When reading a signed character, the value will range from -0x7F to 0x80. An unsigned char is interpreted from 0x00 to 0xFF and is usually what you want for raw bytes.

Edit:

char aChar;

to

unsigned char aChar;

and it should work.

+9
source

Using a character constant will avoid any problems with certain types of signed / unsigned characters:

if( aChar == '\xee' )
+6

Source: https://habr.com/ru/post/1723574/


All Articles