新手问题。
例如,我有十六进制数 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;
谢谢
新手问题。
例如,我有十六进制数 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;
谢谢
用作0xff
掩码以删除除 8 位之外的所有数字:
unsigned int number = 0xABCDEF
unsigned int ef = number & 0xff;
unsigned int cd = (number>>8) & 0xff;
unsigned int ab = (number>>16) & 0xff;
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);
要使用的掩码是0xFF
,不是0x11
。除此之外,你是对的。
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);