2

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
4

3 回答 3

8
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.

于 2008-11-02T20:50:11.463 回答
3
unsigned short t = 0xd790;

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

由于 char 的最大值为 0xFF,因此您不能使用它来比较十六进制数字多于 2 的任何内容。

于 2008-11-02T21:05:50.373 回答
0

由于计算机上的十六进制只不过是一种打印数字(如十进制)的方式,因此您也可以与普通的旧基 10 整数进行比较。

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

更具可读性。

于 2008-11-02T21:16:51.973 回答