[問題] 關於UNION和16進位問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
linux
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
輸出結果跟我想的不一樣
餵入的資料(Input):
無
預期的正確結果(Expected Output):
12ab34cd
0xcd 0x34 0xab 0x12
錯誤結果(Wrong Output):
無
程式碼(Code):(請善用置底文網頁, 記得排版)
1 #include <stdio.h>
2
3 union endian
4 {
5 int i;
6 unsigned char bytes[4];
7 };
8 union endian order;
9
10 int main(int argc,char* argv[])
11 {
12 int i;
13 order.i=0x12ab34cd;
14
15 printf("%x\n",order.i);
16
17 for(i= 0;i<sizeof(int);i++)
18 printf("0x%x\t",order.bytes[i]);
19
20 printf("\n");
21 return 0;
22 }
補充說明(Supplement):
印出結果是從0xcd開始往回印
可是為何不是從0x12開始印???
此外,若改變第五行的unsigned char為char
cd和ab印出來分別變成0xffffffcd,0xffffffab
感覺我的問題蠻簡單的
可是實在想不出為啥= =a
請各位前輩給個hint
謝謝!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.3.96
→
05/03 18:49, , 1F
05/03 18:49, 1F
→
05/03 18:51, , 2F
05/03 18:51, 2F
推
05/03 20:04, , 3F
05/03 20:04, 3F
→
05/04 11:07, , 4F
05/04 11:07, 4F
→
05/04 11:08, , 5F
05/04 11:08, 5F
→
05/04 11:08, , 6F
05/04 11:08, 6F
→
05/04 11:52, , 7F
05/04 11:52, 7F
→
05/04 12:35, , 8F
05/04 12:35, 8F
→
05/04 13:22, , 9F
05/04 13:22, 9F
→
05/04 13:50, , 10F
05/04 13:50, 10F
→
05/04 13:51, , 11F
05/04 13:51, 11F