C计划中的钻头操作和Endianness

发布于 2025-01-19 10:20:17 字数 1359 浏览 2 评论 0原文

我很难理解我的计算机的确切方式,具体取决于dandianness。 我读过此线程 and 本文在两个来源中描述的几个程序,所有这些程序似乎都输出了我的机器确实很小的Endian)。 我有以下宏定义使用SDL和交换2和4个字节值,以防:

#if SDL_BYTEORDER == SDL_BIG_ENDIAN

#define HTON16(n) (n)
#define NTOH16(n) (n)
#define HTON32(n) (n)
#define NTOH32(n) (n)
#define HTON64(n) (n)
#define NTOH64(n) (n)
#else
    
#define HTON16(n) SDL_Swap16(n)
#define NTOH16(n) SDL_Swap16(n)
#define HTON32(n) SDL_Swap32(n)
#define NTOH32(n) SDL_Swap32(n)
#define HTON64(n) SDL_Swap64(n)
#define NTOH64(n) SDL_Swap64(n)
#endif

我的问题是: 在将2个字节编号(在这种情况下为43981 = 0xABCD)写到char [],例如在条目0时,以下代码将在Little Endian(即0xcdab)中产生前2个字节的数据做相反的事情:

char data[100];
int host_value = 43981; // 0xabcd
int net_value = HTON16(host_value);

data[0] = (net_value & 0xff00) >> 8;
data[1] = (net_value & 0xff);

我对先前问题的解决方案是不在主机值上使用Hton16,而是用它来操作,就好像我的机器是大的Endian一样。 另外,在同一台计算机中,在执行以下操作以将相同的主机值编写为数据时,它确实会产生数据,以将两个第一个字节设置为0xABCD:

*((unsigned short *)&data[0]) = HTON16(host_value);

我想理解为什么这两种情况的工作方式有所不同。任何帮助都将受到赞赏。

I am having trouble trying to understand how exactly is my computer doing bitwise operations depending on endianness.
I've read this thread and this article and I think I have confirmed my machine works in little endian (I have tried several programs described in both sources and all of them seem to output my machine is indeed little endian).
I have the following macros defined that use SDL and swap 2 and 4 byte values in case needed:

#if SDL_BYTEORDER == SDL_BIG_ENDIAN

#define HTON16(n) (n)
#define NTOH16(n) (n)
#define HTON32(n) (n)
#define NTOH32(n) (n)
#define HTON64(n) (n)
#define NTOH64(n) (n)
#else
    
#define HTON16(n) SDL_Swap16(n)
#define NTOH16(n) SDL_Swap16(n)
#define HTON32(n) SDL_Swap32(n)
#define NTOH32(n) SDL_Swap32(n)
#define HTON64(n) SDL_Swap64(n)
#define NTOH64(n) SDL_Swap64(n)
#endif

My problem is:
When writing a 2 byte number (in this case 43981 = 0xabcd) to a char[], say, at the entry 0, the following code would produce the first 2 bytes of data in little endian, i.e. 0xcdab, when I'm trying to do the opposite thing:

char data[100];
int host_value = 43981; // 0xabcd
int net_value = HTON16(host_value);

data[0] = (net_value & 0xff00) >> 8;
data[1] = (net_value & 0xff);

My solution to the previous problem is just not using HTON16 on the host value, and operating with it as if my machine was big endian.
Also, in the same machine, when doing the following to write the same host value to data, it does produce data to have the two first byte set to 0xabcd:

*((unsigned short *)&data[0]) = HTON16(host_value);

I would like to understand why these two cases work differently. Any help is appreciated.

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

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

发布评论

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

评论(1

人间☆小暴躁 2025-01-26 10:20:17

问题是这两行,

data[0] = (net_value & 0xff00) >> 8;
data[1] = (net_value & 0xff);

如果您在小型机器上运行

  • (net_value& 0xff00)等同于获得第二个字符,则应该想要第一个字符
  • 是第一个字符,

(net_value& 0xff)是第一个字符,如果您 使用位操作,应该是直接操作,无需致电Hton16

The problem is these two lines,

data[0] = (net_value & 0xff00) >> 8;
data[1] = (net_value & 0xff);

If you run on a little-endian machine,

  • (net_value & 0xff00) is equivalent to getting the second character, you should want the first character
  • (net_value & 0xff) is the first character,

if you use bit operations, it should be a direct operation, no need to call HTON16

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