需要wchar_t的数据字节序的转换? [英] Is endian conversion required for wchar_t data?

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

问题描述

在C / C ++,如果一个多字节宽字符(wchar_t的)值从大端系统传送到小端系统(或反之亦然),将它出来的值相同的其他侧?或将字节需要被换?

In C/C++, if a multi-byte wide character (wchar_t) value is transmitted from a big-endian system to a little-endian system (or vice-versa), will it come out the same value on the other side? Or will the bytes need to be swapped?

推荐答案

是的,你将需要交换他们。结果
字节将从它们放入相同的顺序传送检索。只需在另一端,这些字节排序具有不同的含义。所以,你需要将它们转换为正确的字节序(就是一个字?)。

Yes you will need to swap them.
The bytes will be retrieved from the transport in the same order they were put in. Just at the other end the ordering of these bytes has a different meaning. So you need to convert them to the correct endian-ness (is that a word?).

尝试和真正的方法是转换运输前为网络字节顺序。然后再转换回在收到主机特定的字节顺序(从网络字节顺序)。

The tried and true method is to convert to network byte order before transport. Then convert back to host specific byte order (from network byte order) on receipt.

一组功能,以帮助endian的转换:

A set of function to help with endian conversion:

ntohs   Convert a 16-bit quantity from network byte order to host byte order
ntohl   Convert a 32-bit quantity from network byte order to host byte order
htons   Convert a 16-bit quantity from host byte order to network byte order
htonl   Convert a 32-bit quantity from host byte order to network byte order

我想补充另一个值得注意的问题。结果
不同的系统使用不同大小的wchar_t所以不要以为sizeof运算(wchar_t的)== 2。

Just to add another note of caution.
Different systems use different size for wchar_t so do not assume sizeof(wchar_t) == 2.

此外每台主机可以使用wchar_t的不同重新presentational格式。结果
为了帮助解决这个大多数系统将文本转换为已知格式传输(UTF-8或UTF-16都是不错的选择)。的另一端的文本转换回主机特定的格式。

Additionally each host may use a different representational format for wchar_t.
To help deal with this most systems convert the text to a known format for transport (UTF-8 or UTF-16 are good choices). The convert the text back to the host specific format at the other end.

您可以看看IBM的重症监护病房这所有这些功能。

You could look at IBM's icu this has all this functionality.

这篇关于需要wchar_t的数据字节序的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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