新手問題。拆分一個十六進制數
說例如,我有十六進制數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;
用面膜會0xFF
,不0x11
。除此之外,你是對的。
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);
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;
}
}
在主: 無符號字符分割[4]; splitByte(split,0xffffffff,4);
十六進制0x0011是二進制0000000000010001,所以掩碼只會給你兩位而不是全八位。 –
什麼是'a',你的意思是'號碼'? –