使用以下代码写入文件
int main(int argc, char * argv[])
{
uint32_t pattern1, pattern2;
int i, times;
sscanf("ae75db0f", "%x", &pattern1);
sscanf("518a24f0", "%x", &pattern2);
FILE * outFile = fopen(argv[1],"wb");
printf ("Pattern 1: %0x \nPattern 2: %0x \n", pattern1, pattern2);
times = 524288; // Write out 4 mB of data
for (i = 0; i < times; i++) {
fwrite(&pattern1, 4, 1, outFile);
fwrite(&pattern2, 4, 1, outFile);
}
fclose (outFile);
}
在命令行我做 xxd 文件 |少
00018c0: f024 8a51 0fdb 75ae f024 8a51 0fdb 75ae .$.Q..u..$.Q..u.
00018d0: f024 8a51 0fdb 75ae f024 8a51 0fdb 75ae .$.Q..u..$.Q..u.
00018e0: f024 8a51 0fdb 75ae f024 8a51 0fdb 75ae .$.Q..u..$.Q..u.
00018f0: f024 8a51 0fdb 75ae f024 8a51 0fdb 75ae .$.Q..u..$.Q..u.
它没有显示应该写入文件本身的“正确”值。
最佳答案
欢迎来到字节顺序的世界:
http://en.wikipedia.org/wiki/Endianness
x86/x64 架构(像现在的大多数架构一样)是小端:这意味着多字节值存储在内存中,最低有效字节在前(低内存地址)。
关于c - 十六进制值的写入顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879664/