简体   繁体   English

如何检查十六进制数是否等于特定的二进制数?

[英]How to check if a hex number is equal to a specific binary number?

Hi I'm trying to do something which should be simple, but I can't figure it out. 嗨,我正在尝试做一些应该很简单的事情,但我无法弄清楚。 I have a pointer to an array of unsigned chars, and I get the first element, which is a hex number. 我有一个指向无符号字符数组的指针,我得到了第一个元素,它是一个十六进制数字。 I want to convert it to binary so I can check if it's equal to a number such as 0x01101000. 我想将其转换为二进制,以便检查是否等于数字,例如0x01101000。

      unsigned char arr[] = {0x25};  //just for example. I am actually using *arr.
      unsigned char byte = arr[0];
      if(( byte & 0x01101000) == //not sure if this is the right way to proceed

Would appreciate some help! 希望能有所帮助! Thanks!! 谢谢!!

See this answer. 看到这个答案。

If you are using GCC then you can use GCC extension for this: int x = 0b00010000; 如果您使用的是GCC,则可以为此使用GCC扩展名:int x = 0b00010000;

So in your case, it would be: 因此,在您的情况下,它将是:

if( byte == 0b01101000 )
...

Be sure to put only 8 bits in your literal though. 不过,请务必在文字中只放入8位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM