什么是在C中转换字符串的字节顺序的可移植方式 [英] What's a portable way of converting Byte-Order of strings in C

查看:98
本文介绍了什么是在C中转换字符串的字节顺序的可移植方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写可与任何可以建立套接字连接的标准客户端(例如telnet客户端)进行通信的服务器

I am trying to write server that will communicate with any standard client that can make socket connections (e.g. telnet client)

它最初是作为回显服务器,它当然不需要担心网络字节顺序.

It started out as an echo server, which of course did not need to worry about network byte ordering.

我熟悉ntohs,ntohl,htons和htonl函数.如果我要传输16位或32位整数,或者要发送的字符串中的字符是2或4个字节的倍数,那么这些参数本身就很棒.

I am familiar with ntohs, ntohl, htons, htonl functions. These would be great by themselves if I were transfering either 16 or 32-bit ints, or if the characters in the string being sent were multiples of 2 or 4 bytes.

我想创建一个对字符串进行操作的函数,例如:

I'd like create a function that operates on strings such as:

str_ntoh(char* net_str, char* host_str, int len)
{
    uint32_t* netp, hostp;
    netp = (uint32_t*)&net_str;
    for(i=0; i < len/4; i++){
         hostp[i] = ntoh(netp[i]);
    }
}

或类似的东西.上面的内容假设字大小为32位.我们不能确定发送机上的字数不是16位还是64位?

Or something similar. The above thing assumes that the wordsize is 32-bits. We can't be sure that the wordsize on the sending machine is not 16-bits, or 64-bits right?

对于客户端程序(例如telnet),在发送数据之前必须使用hton *,在接收数据之后必须使用ntoh *,对吗?

For client programs, such as telnet, they must be using hton* before they send and ntoh* after they receive data, correct?

对于那些人来说,因为1字符是一个字节,字节顺序无关紧要:

For the people that thing because 1-char is a byte that endian-ness doesn't matter:

int main(void)
{
    uint32_t a = 0x01020304;
    char* c = (char*)&a;
printf("%x %x %x %x\n", c[0], c[1], c[2], c[3]);

}

运行此代码段.对我来说,输出如下:

Run this snippet of code. The output for me is as follows:

$ ./a.out
  4 3 2 1

使用powerPC芯片组的人应该获得"1 2 3 4",但是使用intel芯片组的人应该看到我获得的大部分收益.

Those on powerPC chipsets should get '1 2 3 4' but those of us on intel chipset should see what I got above for the most part.

推荐答案

也许我在这里丢失了一些东西,但是您要发送字符串,即字符序列吗?然后,您不必担心字节顺序.这仅适用于整数形式的位模式.字符串中的字符始终按正确"顺序.

Maybe I'm missing something here, but are you sending strings, that is, sequences of characters? Then you don't need to worry about byte order. That is only for the bit pattern in integers. The characters in a string are always in the "right" order.

Derrick,为解决您的代码示例,我在Intel i7(小端)和旧的Sun Sparc(大端)上运行了以下(略有扩展)版本的程序

Derrick, to address your code example, I've run the following (slightly expanded) version of your program on an Intel i7 (little-endian) and on an old Sun Sparc (big-endian)

#include <stdio.h>
#include <stdint.h> 

int main(void)
{
    uint32_t a = 0x01020304;
    char* c = (char*)&a;
    char d[] = { 1, 2, 3, 4 };
    printf("The integer: %x %x %x %x\n", c[0], c[1], c[2], c[3]);
    printf("The string:  %x %x %x %x\n", d[0], d[1], d[2], d[3]);
    return 0;
}

如您所见,我已经为您的整数打印输出添加了一个真正的char数组.

As you can see, I've added a real char array to your print-out of an integer.

小端英特尔i7的输出:

The output from the little-endian Intel i7:

The integer: 4 3 2 1
The string:  1 2 3 4

还有大端太阳的输出:

The integer: 1 2 3 4
The string:  1 2 3 4

您的多字节整数确实以不同的字节顺序存储在两台计算机上,但是char数组中的字符具有相同的顺序.

Your multi-byte integer is indeed stored in different byte order on the two machines, but the characters in the char array have the same order.

这篇关于什么是在C中转换字符串的字节顺序的可移植方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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