1
我嘗試使用char指針遍歷整數。我的問題是:爲什麼它向後存儲在內存中?當我運行代碼:在C++中按字節遍歷整數
#include <iostream>
using namespace std;
int main(int count, char** args)
{
unsigned int number = 0xabcdef12;
cout << "Number:\t\t" << hex << number << endl;
cout << "Iterated:\t";
unsigned char* pointer = (unsigned char*)&number;
for(int i=0; i<sizeof(number); i++)
cout << hex << (unsigned int)pointer[i];
cout << endl;
return 0;
}
輸出是:
Number: abcdef12
Iterated: 12efcdab
我想知道是否有辦法迫使C++命令字節,如:
Number: abcdef12
Iterated: abcdef12
(額外信息:我想這樣做,因爲我想通過字節遍歷struct
並將結果寫入文件。)
查找字節順序。 – 2014-09-05 04:53:50
很多有趣的信息在這裏:http://programmers.stackexchange.com/questions/95556/what-is-the-advantage-of-little-endian-format – 2014-09-05 05:07:39