Windows WCHAR 到 Linux wchar_t 字符类型的转换

1、简介
这几天涉及到一个小项目,Windows 和Linux通过TCP进行通信,但是他们之间的字符类型上是有区别的,所有Windows上的数据发送到Linux上后,如果不经过转换,会出现乱码的情况,Windows 的WCHAR类型实际上是wchar_t,但是它只占用2个字节(既UTF-16),而Linux上wchar_t则使用的是多字节(UTF-8),下面的函数提供了他们之间的相互转换!

2、程序代码


#ifdef WINDOWS
#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#else
#include <wchar.h>
#include <string.h>
#endif

#ifdef WIN32
#define USES_WCSUTF16_CONV
#define WCS2UTF16(wcs) (wcs)
#define UTF162WCS(utf16) (utf16)
如下报错,如何解决 In file included from src/SM2.cc:1: In file included from src/SM2.h:3: In file included from incs/gmp.h:34: In file included from /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/iosfwd:99: In file included from /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/__mbstate_t.h:29: /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:141:77: error: use of undeclared identifier 'wcschr' wchar_t* __libcpp_wcschr(const wchar_t* __s, wchar_t __c) {return (wchar_t*)wcschr(__s, __c);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:148:87: error: use of undeclared identifier 'wcspbrk' wchar_t* __libcpp_wcspbrk(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcspbrk(__s1, __s2);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:155:78: error: use of undeclared identifier 'wcsrchr'; did you mean 'wcschr'? wchar_t* __libcpp_wcsrchr(const wchar_t* __s, wchar_t __c) {return (wchar_t*)wcsrchr(__s, __c);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:143:16: note: 'wcschr' declared here const wchar_t* wcschr(const wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:162:86: error: use of undeclared identifier 'wcsstr'; did you mean 'wcschr'? wchar_t* __libcpp_wcsstr(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcsstr(__s1, __s2);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:145:16: note: 'wcschr' declared here wchar_t* wcschr( wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:162:86: error: no matching function for call to 'wcschr' wchar_t* __libcpp_wcsstr(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcsstr(__s1, __s2);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:145:16: note: candidate disabled: <no message provided> wchar_t* wcschr( wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);} ^ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:162:93: error: cannot initialize a parameter of type 'wchar_t *' with an lvalue of type 'const wchar_t *' wchar_t* __libcpp_wcsstr(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcsstr(__s1, __s2);} ^~~~ /home/lzw/ohos-sdk/linux/native/llvm/include/c++/v1/wchar.h:145:38: note: passing argument to parameter '__s' here wchar_t* wcschr( wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);}
最新发布
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值