将Unicode字符转换为wstring
Unicode char to wstring
我正在尝试将C#字符串发送到C++wstring数据,反之亦然。(通过TCP(。
我成功地从C#(如Unicode、UTF-16(发送了字符串数据,并在C++中通过char数组将其输入。
但我不知道如何将char数组转换为wstring。
这就是当c++用utf-16 获得"abcd"时的样子
[0] 97 'a' char
[1] 0 ' ' char
[2] 98 'b' char
[3] 0 ' ' char
[4] 99 'c' char
[5] 0 ' ' char
[6] 100 'd' char
[7] 0 ' ' char
这就是当c++获得"한글"使用utf-16
[0] 92 '' char
[1] -43 '?' char
[2] 0 ' ' char
[3] -82 '?' char
这就是当c++"日本語"使用utf-16
[0] -27 '?' char
[1] 101 'e' char
[2] 44 ',' char
[3] 103 'g' char
[4] -98 '?' char
[5] -118 '?'char
由于UTF-8不支持所有的日语字符,我尝试通过UTF-16(C#字符串基本上使用的(获取数据。但是,通过使用我发现的所有方法,我都未能将这些char数组转换为wstring。
这是我在之前尝试的
std::wstring_convert<std::codecvt_utf16<wchar_t>> myconv
-> what wchar have to have
[0] 54620 '한' wchar_t
[1] 44544 '글' wchar_t
->What it have after using this
[0] 23765 '峕' wchar_t
[1] 174 '®' wchar_t
/
std::wstring wsTmp(s.begin(), s.end());
-> what wchar have to have
[0] 54620 '한' wchar_t
[1] 44544 '글' wchar_t
->What it have after using this
[0] 92 '' wchar_t
[1] 65493 'ᅰ' wchar_t
[2] 0 ' ' wchar_t
[3] 65454 'ᆴ' wchar_t
在这两种情况下,我都将char数组更改为字符串,并将其更改为wstring结果失败了。。。。。。
有人知道如何将非英语UTF-16字符数据转换为wstring数据吗?
添加:C#侧代码
byte[] sendBuffer = Encoding.Unicode.GetBytes(Console.ReadLine());
clientSocket.Send(sendBuffer);
它转换한글'转换成类似字节的
[0] 92 byte
[1] 213 byte
[2] 0 byte
[3] 174 byte
我尝试将C#字符串数据发送到C++wstring数据,反之亦然。(通过TCP(
我成功地从C#(如Unicode、UTF-16(发送了字符串数据,并通过char数组在C++中获得了它。
使用UTF-8而不是UTF-16传输数据会更好,也更便携。
但我不知道如何将char数组转换为wstring。
在wchar_t
为16位的平台上,例如Windows(我认为您使用的是C#(,您可以直接将char
数组内容原样复制到std::wstring
中,例如:
char *buffer = ...;
int buflen = ...;
std::wstring wstr(reinterpret_cast<wchar_t*>(buffer), buflen / sizeof(wchar_t));
如果您需要支持wchar_t
为32位的平台,则可以使用std::wstring_convert
:
char *buffer = ...;
int buflen = ...;
std::wstring_convert<std::codecvt_utf16<wchar_t>, wchar_t> conv;
std::wstring wstr = conv.from_bytes(std::string(buffer, buflen));
// or:
// std::wstring wstr = conv.from_bytes(buffer, buffer+buflen);
由于wchar_t
不是很可移植,请考虑使用std::u16string
/char16_t
(如果您使用的编译器支持C++11或更高版本(,因为它们是专门为UTF-16数据设计的。
由于UTF-8不支持所有日语字符
是的,确实如此。Unicode是实际的字符集,UTF-8只是将Unicode代码点表示为字节序列的编码ALLUTF(UTF-7、UTF-8、UTF-16和UTF-32(支持ENTIREUnicode字符集,并且UTF的设计允许从一个UTF到另一个的无损耗转换。
- 将日语 wstring 转换为 std::string
- 如何将std::wstring转换为char const[]
- 将utf16宽std::wstring转换为utf8窄std::string以获得罕见字符时出现问题
- 在 C++ 中将 utf8 wstring 转换为窗口中的字符串
- 如何将 wstring 转换为 wchar_t*?C++
- 从 std::wstring 转换为 jstring
- 正在将std::wstring转换为const wchar_t x[]
- 使用字符串构造函数从wstring转换为字符串时,是否没有可能的数据丢失
- 将 std::wstring 转换为 SQLWCHAR *
- C++错误(将 wstring 转换为字符串):无法将参数 1 从 std::wchar_t 转换为 std::char
- 通过错误检查将 wstring 转换为双精度的方法
- 如何将wstring转换为字节向量
- C++:如何将带有 md5 哈希的 wstring 转换为字节* 数组
- 如何将std::wstring转换为LSA_UNICODE_STRING
- 将"wstring"转换为"const UInt8 *"
- 从Wstring转换为CComBstr
- 将wstring转换为Unicode
- 将std::wstring转换为cstring安全吗?
- 在Linux上将wstring转换为jstring
- 将wstring*转换为PWSTR