【发布时间】:2013-04-08 11:14:14
【问题描述】:
发送和接收字符串时是否需要转换为网络/主机字节顺序。可用的函数(例如 htons())仅适用于 16 位和 32 位整数。我也知道一个 char 不应该有所作为的事实,一般来说,它是一个字节大。但是字符串呢?
下面是代码sn-p
int len; recv(fd, &len, sizeof (int), 0);
len = ntohl(len);
char* string = malloc(sizeof (char) * (len + 1));
int received = recv(fd, string, sizeof (char) * len, 0);
string[len] = '\0';
【问题讨论】:
-
纯ascii字符串还是unicode?span>
标签: c endianness