如何将INT32_T的4个字节写入C中的Big-Endian订单的二进制文件?

发布于 2025-01-28 18:04:00 字数 536 浏览 3 评论 0原文

我想将int32_t的4个字节写入大端顺序的二进制文件。我直接使用了fwrite()和指向我的int32_t的指针,但它有些可行,但问题是我的整数是用小序列编写的,字节先写。例如,如果我写的话:

int32_t specialInt = 262;
fwrite(&specialInt, 4, 1, myFile);

我会用十六进制编辑器打开它,我会看到:

06 01 00 00 ...

与我想要的方式相比,这是倒退的。我想要:

00 00 01 06 ...

如何使我的int_32t按大阶订单?是否有一个内置的C库函数可以以正确的顺序获取字节,或者我应该使用memcpy()将字节放入温度char阵列中,然后一一写入字节向后倒入文件?

I want to write the 4 bytes of an int32_t to a binary file in big-endian order. I used fwrite() directly with a pointer to my int32_t, and it somewhat works, but the problem is that my integer is written in little-endian order, with the smallest bytes written first. For example, if I write:

int32_t specialInt = 262;
fwrite(&specialInt, 4, 1, myFile);

and I open it with my hex editor, I see:

06 01 00 00 ...

which is backwards compared to how I want it. I would like:

00 00 01 06 ...

How should I get my int_32t to be in big-endian order? Is there a built-in C library function that will get the bytes in the correct order, or should I use memcpy() to put the bytes into a temp char array, then write the bytes one by one backwards into the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深缘浅 2025-02-04 18:04:00

感谢PMG在评论中写下答案:

假设char_bit == 8

unsigned char value[4];
value[0] = (uint32_t)specialInt >> 24;
value[1] = (uint32_t)specialInt >> 16;
value[2] = (uint32_t)specialInt >> 8;
value[3] = (uint32_t)specialInt;
fwrite(value, 4, 1, myFile);

Thanks pmg for writing the answer in the comment:

Assuming CHAR_BIT == 8:

unsigned char value[4];
value[0] = (uint32_t)specialInt >> 24;
value[1] = (uint32_t)specialInt >> 16;
value[2] = (uint32_t)specialInt >> 8;
value[3] = (uint32_t)specialInt;
fwrite(value, 4, 1, myFile);
爱的十字路口 2025-02-04 18:04:00

您可以在POSIX 2001标准中使用htonl()函数arpa/inet.h标题。参见 https://linux.die.net/man/3/ntohl

是互联网字节订单,称为“网络字节订单”。

您需要将int32_t转换为uint32_t。该铸件由C标准定义。接下来,它通过htonl()将其转换为网络endian,然后将其写入文件:

int32_t specialInt = 262;
uint32_t encodedInt = htonl((uint32_t) specialInt);
_Static_assert(sizeof encodedInt == 4, "Oops");
fwrite(&encodedInt, 4, 1, myFile);

可以用复合文字来缩写一点。

fwrite(&(uint32_t) { htonl((uint32_t)specialInt) }, 4, 1, myFile);

You could use htonl() function from POSIX 2001 standard available in arpa/inet.h header. See https://linux.die.net/man/3/ntohl

Big endian is Internet byte order, known as "network byte order".

You need to transform int32_t to uint32_t. This cast is defined by C standard. Next transform it to network endian via htonl() and then write it to a file:

int32_t specialInt = 262;
uint32_t encodedInt = htonl((uint32_t) specialInt);
_Static_assert(sizeof encodedInt == 4, "Oops");
fwrite(&encodedInt, 4, 1, myFile);

It could be abbreviated a bit with a compound literal.

fwrite(&(uint32_t) { htonl((uint32_t)specialInt) }, 4, 1, myFile);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文