用于Linux和Windows的wchar_t之间的差异和转换 [英] Difference and conversions between wchar_t for Linux and for Windows

查看:346
本文介绍了用于Linux和Windows的wchar_t之间的差异和转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个 在Windows中,wchar_t是16位&对于Linux,wchar_t是32位。



我有一个客户机 - 服务器体系结构(只使用管道 - 不是套接字) - 我的服务器是基于Windows的,客户机是Linux。

服务器有一个API来从客户端检索主机名。当客户端是基于Windows时,它可以执行GetComputerNameW并返回宽字符串。
然而,当客户端是基于Linux的时候,事情会变得混乱。



作为第一种天真的方法,我使用mbstowcs()希望将wchar_t *返回给Windows服务器端。
然而,这个LPWSTR(我在linux的clinet端有typedef wchar_t * LPWSTR)在Windows上是不可识别的,因为它预计它的wchar_t是16位。



<因此,将linux上的gethostname()的输出转换为char *为unsigned short(16-bit)唯一选项?

在此先感谢!

解决方案

您必须决定如何通过线路传输数据的实际协议。这里有几个选项,虽然可能UTF-8通常是最明智的选择 - 也就是说,在linux下你基本上可以直接使用数据(没有理由使用wchar_t开始,尽管你显然可以将它转换成任何你在Windows下,你必须将UTF-8转换为UTF-16(是的,不完全是,但是哦),哪些窗口想要,如果你想要发送数据您必须将其转换为UTF-8。幸运的是,Windows提供了分别这个函数完全用于这些目的。

显然你可以决定你想要的任何编码不一定是UTF-8,这个过程是一样的:当接收到的数据将它转换成操作系统的本地格式时,发送时将其转换为您的在线编码。如果您不使用utf-8, iconv 适用于linux。


I understand from this and this thread that in Windows, wchar_t is 16-bit & for Linux, wchar_t is 32 bit.

I have a client-server architecture (using just pipes - not sockets)- where my server is Windows based and client is Linux.

Server has a API to retrieve hostname from client. When the client is Windows based, it could just do GetComputerNameW and return Wide-String. However, when the client is Linux based, things get messy.

As a first naive approach, I used mbstowcs() hoping to return wchar_t* to Windows server-side. However, this LPWSTR (I have typedef wchar_t* LPWSTR on my linux clinet side) is not recognizable on Windows since it expects its wchar_t to be 16-bit.

So, converting the output of gethostname() on linux - which is in char* to unsigned short (16-bit) my only option?

Thanks in Advance!

解决方案

You will have to decide on the actual protocol on how to transport the data across the wire. Several options here although probably UTF-8 is usually the most sensible one - also that means that under linux you can basically just use the data as-is (no reason to use wchar_t to begin with, although you obviously can convert it into whatever you want).

Under Windows you will have to convert the UTF-8 into UTF-16 (yes not exactly, but oh well) which windows wants and if you want to send data you have to convert it to UTF-8. Luckily windows provides this respectively this function for exactly these purposes.

Obviously you can decide on any encoding you want to not necessarily UTF-8, the process is the same: When receiving data convert it to the native format of the OS, when sending convert it to your on-wire encoding. iconv works on linux if you don't use utf-8.

这篇关于用于Linux和Windows的wchar_t之间的差异和转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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