BobS
BobS

Reputation: 567

Check if Char is in range

If I have a range of say 000080-0007FF and I want to see if a char containing hex is within that range, how can I do it?

Example

char t = 0xd790;

if (t is within range of 000080-0007FF) // true

Upvotes: 2

Views: 3360

Answers (3)

dicroce
dicroce

Reputation: 46770

Since hex on a computer is nothing more than a way to print a number (like decimal), you can also do your comparison with plain old base 10 integers.

if( (t >= 128) && (t <= 2047) )
{
}

More readable.

Upvotes: 0

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

unsigned short t = 0xd790;

if (t >= 0x80 && t <= 0x7ff) ...

Since a char has a max value of 0xFF you cannot use it to compare anything with more hex digits than 2.

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 993015

wchar_t t = 0xd790;

if (t >= 0x80 && t <= 0x7ff) ...

In C++, characters are interchangeable with integers and you can compare their values directly.

Note that I used wchar_t, because the char data type can only hold values up to 0xFF.

Upvotes: 8

Related Questions