[英]Concatenating hex bytes in C
我試圖將4個十六進制字節連接在一起,並且遇到了一些麻煩。
我有一個32位數字,表示為4個十六進制字節( x
)。 我還有另一個4位數字,我將2的補碼作為( twos_complement
),然后將其轉換為十六進制表示形式( ne
)。 然后,我想將單個字節( ne
)細分為原始32位數字( x
)的第三個字節。 這是我到目前為止的內容:
unsigned replace_byte(unsigned x, unsigned char b) {
unsigned new;
unsigned int twos_complement;
twos_complement = (~b) +1;
unsigned int ne = (twos_complement & 0xff);
unsigned int one = (x >> 24) & 0xff;
unsigned int two = (x >> 16) & 0xff;
unsigned int three = (x >> 8) & 0xff;
unsigned int four = x & 0xff;
printf("one 0x%x, two 0x%x, three 0x%x, four 0x%x\n",one, two, three, four);
new = (one<<24) | (two<<16) | (ne) | (four) ;
printf("new 0x%x", new);}
當我為i
和3輸入11123243336作為b
,得到的十六進制值為i = 0x96ff3948
和b = 0xfffffffd
。 當我跑,我得到new
的0x96ff00fd
時,即時通訊期待0x96fffd48
。 任何幫助表示贊賞!
對我來說,您要實現的目標還不是很清楚,但是從您的預期中我的猜測是,您只需要將ne
移位8位,因此:
new = (one<<24) | (two<<16) | (ne<<8) | (four) ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.