C语言 如何使用C比较十六进制值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7767427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:54:14  来源:igfitidea点击:

How to compare hex values using C?

chex

提问by mainajaved

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?

我怎样才能做到这一点?

回答by Paul R

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 '?'.

请注意,无需将任何内容“转换”为十六进制 - 您可以直接比较字符或整数值,因为 0x3f 等十六进制常量只是表示整数值的另一种方式。0x3f == 63(十进制)== ASCII '?'。

回答by Shahbaz

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.

计算机中的数字都是0和1。以基数 10 或基数 16 (hex) 或作为字符(例如 'a')查看它们不会改变数字。

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)

回答by sehe

Yes you can:

是的你可以:

 if  (buffer[i] > 0x3F)

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

(注意小写的 x)。编辑结果证明0X3F应该也能正常工作,但我很想说这不是 C 程序员通常编写的内容)。

回答by ericcurtin

When comparing char to hex you must be careful:

将 char 与 hex 进行比较时,您必须小心:

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

使用 == 运算符将字符与 0x80 进行比较总是导致错误?

I would recommend this syntax introduced in C99 to be sure

我会推荐 C99 中引入的这种语法,以确保

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.

它告诉编译器 0x3f 是一个字符而不是一个 int(类型安全),否则你很可能会看到这个比较的问题。

In fact clang compiler will warn you about this:

事实上,clang 编译器会警告你这一点:

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

常量 128 与类型 'char' 表达式的比较始终为 false [-Werror,-Wtautological-constant-out-of-range-compare]

回答by Jeegar Patel

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

十六进制值并不是什么新数据类型,它只是另一种数字方法,例如

int a = 10;

int a = 10;

in Hex printing

十六进制印刷

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

output is

输出是

a in hex value A

in if loop

在 if 循环中

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 循环中

if(a == 10)
    do something