字节顺序在C大字符数组 [英] Byte order with a large array of characters in C

查看:127
本文介绍了字节顺序在C大字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C /网络新手的问题...

Hey guys, question from a C/Networking newbie...

我在做一些C套接字编程,并试图用字节顺序问题搏斗。我的要求(发送)是好的,但是当我收到我的数据字节全乱套了。我开始这样的事情...

I'm doing some socket programming in C and trying to wrestle with byte order problems. My request (send) is fine but when I receive data my bytes are all out of order. I start with something like this...

char * aResponse= (char *)malloc(512);
int total = recv(sock, aResponse, 511, 0);

在这个响应处理,每16位词似乎有它的逆转字节(我使用的是UDP)。我试图做这样的事情...

When dealing with this response, each 16bit word seems to have it's bytes reversed (I'm using UDP). I tried to fix that by doing something like this...

    unsigned short * _netOrder= (unsigned short *)aResponse;
    unsigned short * newhostOrder= (unsigned short *)malloc(total);
    for (i = 0; i < total; ++i)
    {
	     newhostOrder[i] = ntohs(_netOrder[i]);
    }

这时候,我处理的数据作为短期工作正常,但是如果我投的指针为char再次字节逆转。我在做什么错了?

This works ok when I'm treating the data as a short, however if I cast the pointer to a char again the bytes are reversed. What am I doing wrong?

谢谢!

推荐答案

好吧,似乎有与你正在做在两个不同的层面有什么问题。这里的混乱的部分似乎干您使用指针,它们指向对象的类型,然后在存储器中的值的编码的帧间pretation由指针(多个)指向的

Ok, there seems to be problems with what you are doing on two different levels. Part of the confusion here seems to stem for your use of pointers, what type of objects they point to, and then the interpretation of the encoding of the values in the memory pointed to by the pointer(s).

多字节实体在存储器中的编码是什么被称为字节序。这两种常见的编码被称为 的Little Endian (LE)和大端(BE)。随着LE,就像一个短暂的16位数量为连接第一codeD至少显著字节(LSB)。下,最显著字节(MSB)为EN codeD第一。

The encoding of multi-byte entities in memory is what is referred to as endianess. The two common encodings are referred to as Little Endian (LE) and Big Endian (BE). With LE, a 16-bit quantity like a short is encoded least significant byte (LSB) first. Under BE, the most significant byte (MSB) is encoded first.

按照惯例,网络协议通常连接code的东西变成我们称之为网络字节顺序(NBO),这也恰好是一样的BE。如果您发送和大型平台接收内存缓冲区,那么你就不会遇到转换问题。但是,您的code然后将平台依赖于BE大会。如果你想写可以移植的code这两个LE正常工作和BE平台,你不应该承担该平台的字节序。

By convention, network protocols normally encode things into what we call "network byte order" (NBO) which also happens to be the same as BE. If you are sending and receiving memory buffers on big endian platforms, then you will not run into conversion problems. However, your code would then be platform dependent on the BE convention. If you want to write portable code that works correctly on both LE and BE platforms, you should not assume the platform's endianess.

实现端便携性套路例如 ntohs和)的目的( ntohl() htons() htonl()。这些函数/宏给定的平台,在发送端和接​​收端做必要的转换上定义的:

Achieving endian portability is the purpose of routines like ntohs(), ntohl(), htons(), and htonl(). These functions/macros are defined on a given platform to do the necessary conversions at the sending and receiving ends:


  • htons() - 从主机顺序到网络顺序(发送)转换short值

  • htonl() - 从主机顺序到网络顺序(发送)转换long值

  • ntohs和() - 从网络为了举办序转换short值(接收之后)

  • ntohl() - 从网络为了举办序转换长值(接收之后)

了解您的什么时候转换回字符在内存中实体的实际顺序没有影响访问存储器评论。也就是说,如果你访问该缓冲区一系列字节,你会看到什么为了他们实际上是连接codeD到内存中的字节数,你是否有一个是或LE机。所以,如果你正在寻找一个NBO连接codeD缓冲区接收之后,MSB将是第一个 - 永远。如果你看一下输出缓冲区您已经转换回后举办秩序,如果您有BE机,字节顺序将保持不变。相反,LE机上,字节都将现在在转换缓冲逆转。

Understand that your comment about accessing the memory when cast back to characters has no affect on the actual order of entities in memory. That is, if you access the buffer as a series of bytes, you will see the bytes in whatever order they were actually encoded into memory as, whether you have a BE or LE machine. So if you are looking at a NBO encoded buffer after receive, the MSB is going to be first - always. If you look at the output buffer after your have converted back to host order, if you have BE machine, the byte order will be unchanged. Conversely, on a LE machine, the bytes will all now be reversed in the converted buffer.

最后,在您的转换循环,变量指的是字节。但是,你正在访问的缓冲区短裤。您的环路保护不应该是,而应该是:

Finally, in your conversion loop, the variable total refers to bytes. However, you are accessing the buffer as shorts. Your loop guard should not be total, but should be:

总/的sizeof(无符号短)

占每个双字节自然

这篇关于字节顺序在C大字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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