简体   繁体   中英

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.

      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;

So in your case, it would be:

if( byte == 0b01101000 )
...

Be sure to put only 8 bits in your literal though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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