字节顺序和整数变量
在 c 中,我不太担心字节顺序的概念。假设我在小端机器上声明一个整数(2 个字节),
int a = 1;
并且它存储为:
Address value
1000 1
1001 0
在大端机器上,它应该存储为反之亦然。现在,如果我这样做 &a 那么我应该在两台机器上获得 1000。
如果这是真的,那么如果我存储 int a=1 那么我应该在小端上得到 a=1 而在大端上得到 2^15 。这是正确的吗?
In c I am little worried with the concept of endianess. Suppose I declare an integer (2 bytes) on a little endian machine as
int a = 1;
and it is stored as:
Address value
1000 1
1001 0
On big endian it should be stored as vice-versa. Now if I do &a then I should get 1000 on on both machines.
If this is true then if I store int a=1
then I should get a=1 on little endian whereas 2^15 on big endian. Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只要您不在平台之间传输数据(或通过汇编代码访问它),数据的表示方式对您来说并不重要。
如果您只使用标准 C - 它对您来说是隐藏的,您不应该为它烦恼。如果您在未知机器之间传递数据(例如,您有一个客户端和一个通过某些网络进行通信的服务器应用程序),请使用从本地转换为本地的
hton
和ntoh
函数网络以及从网络到本地字节顺序,以避免出现问题。如果直接访问内存,那么不仅会出现字节序问题,还会出现打包问题,所以要小心。
It should not matter to you how the data is represented as long as you don't transfer it between platforms (or access it through assembly code).
If you only use standard C - it's hidden from you and you shouldn't bother yourself with it. If you pass data around between unknown machines (for example you have a client and a server application that communicate through some network) - use
hton
andntoh
functions that convert from local to network and from network to local endianess, to avoid problems.If you access memory directly, then the problem would not only be endian but also packing, so be careful with it.
在小端和大端中,都会返回“第一个”字节的地址。这里的“第一个”是指示例中的 1000,而不是最高有效字节或最低有效字节意义上的“第一个”。
In both little endian and big endian, the address of the "first" byte is returned. Here by "first" I mean 1000 in your example and not "first" in the sense of most-significant or least significant byte.
不;在两台机器上您都会得到地址 1000。
不;因为初步条件是假的。另外,如果将 1 赋给变量,然后再次读回该值,则会得到结果 1。其他任何事情都会是造成混乱的主要原因。 (有时无法保证这一点(读回存储的值) - 例如,如果多个线程可能正在修改变量,或者它是嵌入式系统上的 I/O 寄存器。但是,您会知道如果您需要了解的话。)
在大多数情况下,您不需要担心字节顺序。有几个地方你可以这样做。套接字网络功能就是其中之一;将二进制数据写入将在不同字节顺序的机器之间传输的文件是另一种方法。几乎所有其他时间,您都不必担心。
No; on both machines you will get the address 1000.
No; because the preliminary condition is false. Also, if you assign 1 to a variable and then read the value back again, you get the result 1 back. Anything else would be a major cause of confusion. (There are occasions when this (reading back the stored value) is not guaranteed - for example, if multiple threads could be modifying the variable, or if it is an I/O register on an embedded system. However, you'd be aware of these if you needed to know about it.)
For the most part, you do not need to worry about endianness. There are a few places where you do. The socket networking functions are one such; writing binary data to a file which will be transferred between machines of different endianness is another. Pretty much all the rest of the time, you don't have to worry about it.
这是正确的,但这不仅仅是 c 中的问题,而是任何读写二进制数据的程序的问题。如果数据保留在一台计算机上,那就没问题了。
另外,如果您从文本文件中读取/写入,这不会成为问题。
This is correct, but it's not only an issue in c, but any program that reads and writes binary data. If the data stays on a single computer, then it will be fine.
Also, if you are reading/writing from text files this won't be an issue.