字节到字节:Endianess值得关注吗? [英] Ints to Bytes: Endianess a Concern?

查看:97
本文介绍了字节到字节:Endianess值得关注吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我是否需要担心字节序(整数必须为0-127):

Do I have to worry about endianness in this case (integers MUST be 0-127):

    int a = 120;
    int b = 100;
    int c = 50;
    char theBytes[] = {a, b, c};

我认为,由于每个整数都位于其自己的字节中,因此在系统之间传递字节数组时,我不必担心Endianess.这也根据经验得出.我想念什么吗?

I think that, since each integer sits in its own byte, I don't have to worry about Endianess in passing the byte array between systems. This has also worked out empirically. Am I missing something?

推荐答案

字节序仅影响单个值中字节的顺序.单个字节不受字节序问题的影响,并且数组始终是顺序的,因此字节数组在大字节序和小字节序体系结构中都是相同的.

Endianness only affects the ordering of bytes within an individual value. Individual bytes are not subject to endian issues, and arrays are always sequential, so byte arrays are the same on big- and little-endian architectures.

请注意,这并不一定意味着仅使用 char s可使数据类型100%字节可移植.例如,结构可能仍包含依赖于体系结构的填充,并且一个系统可能具有未签名的 char ,而另一个系统则使用了已签名(尽管我看到您只允许使用0-127来回避这一点).

Note that this doesn't necessarily mean that only using chars will make datatypes 100% byte-portable. Structs may still include architecture-dependent padding, for example, and one system may have unsigned chars while another uses signed (though I see you sidestep this by only allowing 0-127).

这篇关于字节到字节:Endianess值得关注吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆