5

新手问题。

例如,我有十六进制数 0xABCDEF,我将如何将其拆分为 0xAB、0xCD 和 0xEF?是这样吗?

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

谢谢

4

4 回答 4

15

用作0xff掩码以删除除 8 位之外的所有数字:

unsigned int number = 0xABCDEF
unsigned int ef = number & 0xff;
unsigned int cd = (number>>8) & 0xff;
unsigned int ab = (number>>16) & 0xff;
于 2012-10-09T11:01:12.710 回答
4
unsigned int number = 0xABCDEF
unsigned int ef = number & 0xff;
unsigned int cd = (number >> 8) & 0xff;
unsigned int ab = (number >> 16) & 0xff;

&您可能希望ef, cd,ab成为类型,而不是按位和 ( ) 操作,unsigned char这取决于您的代码的其余部分以及您如何使用这些变量。在这种情况下,您转换为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);
于 2012-10-09T11:03:18.857 回答
0

要使用的掩码是0xFF,不是0x11。除此之外,你是对的。

于 2012-10-09T11:01:23.990 回答
0
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;

  }
}

主要是: unsigned char split[4]; splitByte(拆分,0xffffffff,4);

于 2014-12-26T15:26:20.927 回答