2012-12-16 141 views
1

我對設置(短整型)的高位和低位字節有問題。 請看我的代碼,它只是返回數字1,但必須返回55。在C++中設置(短整型)的高位和低位字節

int number = 55; 
int mynumber = 0; 

//convert short int to char. 
char upper = number >> 8; 
char lower = number && 8; 

//convert char to short int . 
mynumber = (mynumber & 0xff00) | lower; 
mynumber = (mynumber & 0x00ff) | (upper << 8); 

printf("%i",mynumber); 
+0

[劈裂一個十六進制到2個進制值]的可能的複製(http://stackoverflow.com/questions/25811662/spliting-an-hex-成-2-十六進制值) – cyberponk

回答

4

lower計算是錯誤的:

char lower = number & 0xff;