8

I am working with hex values. Until now I know how to print hex values and also precision thing. Now I want to compare the hex values. For example I am reading data from a file into a char buffer. Now I want to compare the hex value of data in the buffer. Is there anything like this?

if  hex(buffer[i]) > 0X3F  
then
//do somthing

How can I do this?

makes
  • 6,438
  • 3
  • 40
  • 58
mainajaved
  • 7,905
  • 10
  • 36
  • 44

5 Answers5

16

You're nearly there:

if (buffer[i] > 0x3f)
{
    // do something
}

Note that there is no need to "convert" anything to hex - you can just compare character or integer values directly, since a hex constant such as 0x3f is just another way of representing an integer value. 0x3f == 63 (decimal) == ASCII '?'.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • ok sir thanks alot . although my buffer contain character values it can be compared with hex? – mainajaved Oct 14 '11 at 12:21
  • 4
    Yes - a char is really just one kind of integer in C and related languages - anything you can do with an int you can also do with a char. – Paul R Oct 14 '11 at 12:23
7

Numbers in the computer are all 0s and 1s. Looking at them in base 10, or base 16 (hex) , or as a character (such as 'a') doesn't change the number.

So, to compare with hex, you don't need to do anything.

For example, if you have

int a = 71;

Then the following two statements are equivalent:

if (a == 71)

and

if (a == 0x47)
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
4

Yes you can:

 if  (buffer[i] > 0x3F)

(note the lowercase x). Edit it turns out 0X3F should work just as well, but I am tempted to say it is not what C programmers usually write).

sehe
  • 374,641
  • 47
  • 450
  • 633
  • ok sir thanks alot . although my buffer contain character values it can be compared with hex? – mainajaved Oct 14 '11 at 12:22
  • hex is only a _representation_. The computer only _stores_ numbers and you _interpret_ them as byte/char/float/string and the way you type them in code or print them to screen is a _text representation_ – sehe Oct 14 '11 at 12:24
  • @another.anon.coward: apparently not. I'm just not used to seeing it uppercased. (_There is a difference when using printf (uppercase leters A-F) - but that's unrelated here_) – sehe Oct 14 '11 at 12:26
  • @sehe: True about `printf`. Also true that generally `X` is not used , but just so that OP knows that both work, in case `X` is seen somewhere instead of `x` :) – another.anon.coward Oct 14 '11 at 12:29
2

When comparing char to hex you must be careful:

Using the == operator to compare a char to 0x80 always results in false?

I would recommend this syntax introduced in C99 to be sure

if (buffer[i] > '\x3f')
{
    // do something
}

It tells the compiler that the 0x3f is a char rather than an int (type-safety), otherwise it is likely you will see issue with this comparison.

In fact clang compiler will warn you about this:

comparison of constant 128 with expression of type 'char' is always false [-Werror,-Wtautological-constant-out-of-range-compare]

ericcurtin
  • 1,499
  • 17
  • 20
0

Hex values are nothing new data types its just another numberical methods like

int a = 10;

in Hex printing

printf("a in hex value %x ",a);

output is

a in hex value A

in if loop

if(a == 0xa)
    do something

in decimal

printf("a in decimal value %d ",a);

output is

a in hex value 10

in if loop

if(a == 10)
    do something
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222