Hexadecimal Division

newbie question.

Say, for example, I have the hexadecimal number 0xABCDEF, how would I divide it into 0xAB, 0xCD and 0xEF? This is true?

unsigned int number = 0xABCDEF unsigned int ef = a & 0x000011; unsigned int cd = (a>>8) & 0x000011; unsigned int ab = (a>>16) & 0x000011; 

thanks

+4
source share
4 answers

Use 0xff as a mask to remove only 8 bits of a number:

 unsigned int number = 0xABCDEF unsigned int ef = number & 0xff; unsigned int cd = (number>>8) & 0xff; unsigned int ab = (number>>16) & 0xff; 
+12
source
 unsigned int number = 0xABCDEF unsigned int ef = number & 0xff; unsigned int cd = (number >> 8) & 0xff; unsigned int ab = (number >> 16) & 0xff; 

Instead of bitwise and ( & ) operations, you might want ef , cd , ab be unsigned char types, depending on the rest of your code and how you work with these variables. In this case, you pass an unsigned char :

 unsigned int number = 0xABCDEF unsigned char ef = (unsigned char) number; unsigned char cd = (unsigned char) (number >> 8); unsigned char ab = (unsigned char) (number >> 16); 
+3
source

The mask used will be 0xFF , not 0x11 . Other than that, you're right.

0
source
 void splitByte(unsigned char * split, unsigned int a,int quantBytes) { unsigned char aux; int i; for(i=0;i<quantBytes;i++) { split[i]=a&0x00FF; a=(a>>8); } for(i=0;i<quantBytes-1;i++) { aux = split[i]; split[i] = split[quantBytes-i-1]; split[quantBytes-i-1] = aux; } } 

Basically: unsigned char split [4]; splitByte (split, 0xffffffff, 4);

0
source

Source: https://habr.com/ru/post/1438629/


All Articles