- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在 std::string
上构建一个合成的 UTF-8 字符串,并尝试使用 MultiByteToWideChar
对其进行转换。这是我的代码:
std::string str;
str += 'A';
str += char(0);
str += 'B';
str += char(0);
str += 'C';
str += char(0);
str += char(0);
str += char(0);
std::wstring wstr;
if (str.empty()){
wstr = L"";
}
int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), NULL, 0) + 1;
std::wstring wstrTo(sizeNeeded, 0);
MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), &wstrTo[0], sizeNeeded);
wstr = wstrTo;
std::wcout << wstr;
事实证明,MultiByteToWideChar
并没有将 str
简单地转换为 L"ABC"字符串,而是将每个字符分别转换为 wchar_t
- 意思是'A' + char(0)
不会变成 L'A'
而是变成 L'A' + L'\0'
我是不是做错了什么,或者这是 MultiByteToWideChar
的预期行为?
最佳答案
std::string str;
str += 'A';
str += char(0);
str += 'B';
str += char(0);
str += 'C';
str += char(0);
str += char(0);
str += char(0);
这不是生成 UTF-8 编码的字符串!它正在生成一个 UTF-16 编码的字符串。
int sizeNeeded = MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), NULL, 0) + 1;
您告诉 MultiByteToWideChar()
将 str
的原始字节解释为 UTF-8,即使它实际上并未以 UTF-8 编码。
Unicode 代码点 U+0000
在 UTF-8 中有效(它被编码为 0x00
),因此 中的每个
将被解释为代码点 0x00
字符>strU+0000
,其余字符将按原样解释,因为它们都小于 U+0080
。因此,在您的“UTF-8”字符串中没有多字节序列,只有单字节序列。
您最终得到一个包含以下代码点的 UTF-16 wstring
:
0x41 -> U+0041
0x00 -> U+0000
0x42 -> U+0042
0x00 -> U+0000
0x43 -> U+0043
0x00 -> U+0000
0x00 -> U+0000
0x00 -> U+0000
如果您将 str
正确编码为 UTF-8,然后将其解释为 UTF-8,您最终会得到正确的 UTF-16 wstring
:
std::string str;
str += 'A';
str += 'B';
str += 'C';
str += char(0);
0x41 -> U+0041
0x42 -> U+0042
0x43 -> U+0043
0x00 -> U+0000
或者,如果您将 str
编码为 UTF-16 并将其解释为 UTF-16(您不能使用 MultiByteToWideChar()
,您将不得不这样做它手动),你仍然会得到一个包含正确代码点的 UTF-16 wstring
:
std::string str;
str += 'A';
str += char(0);
str += 'B';
str += char(0);
str += 'C';
str += char(0);
str += char(0);
str += char(0);
0x41 0x00 -> U+0041
0x42 0x00 -> U+0042
0x43 0x00 -> U+0043
0x00 0x00 -> U+0000
关于c++ - MultiByteToWideChar 不能很好地转换我的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30278849/
我在 Visual Studio 9.0 上有一个 Visual C++ 应用程序。我们已经使用“Unicode character Set”作为 Character Set 构建了应用程序。我们正在
我有像这样使用 MultiByteToWideChar 的代码: wchar_t * bufferW = malloc(mbBufferLen * 2); MultiByteToWideChar(CP
我正在使用 MultiByteToWideChar 将我的字符串转换为 wstring。我首先尝试为我的 wstring 获取所需的大小。根据将 0 作为最后一个参数传递的文档应该可以完成此操作。使用
以下代码打印了所需的输出,但它在字符串末尾打印了乱码。最后一次调用 MultiByteToWideChar 有问题,但我不知道是什么。请帮忙?? #include "stdafx.h" #includ
此韩语文本(可打印引用)“2013-03-22 =0E?@HD=0F 05:30”无法通过 MultiByteToWideChar 正确转换为 Unicode。 Quoted-printable fo
我正在 std::string 上构建一个合成的 UTF-8 字符串,并尝试使用 MultiByteToWideChar 对其进行转换。这是我的代码: std::string str; str +=
我想将普通的string 转换为wstring。为此,我尝试使用 Windows API 函数 MultiByteToWideChar .但这对我不起作用。 这是我所做的: string x = "T
正如 man 3p mbrtowc 所说,它可能返回四种结果类型:0(如果字节将被解码为 NUL 宽字符) , (size_t)-2 如果序列被截断,(size_t)-1 如果序列非法,以及从零到多字
这是函数: char *ToLowerSTR(/*char*/ WCHAR* buffer) // NEED TO FIX UNRESOLVED EXTERNAL { CHAR* str;
前几天在开发程序时,我必须将 ASCII 字符串转换为 Unicode 字符串。顺便说一句,我正在使用 Visual Studio 2012 在 Windows 上工作。我注意到 Win32 函数 M
我有一个 Excel VBA 项目,我正在适应 64 位 Office。一方面,我调用 MultiByteToWideChar()使用 20 个左右不同的代码页中的任何一个。 (所以 StrConv
我想从 UTF-8 文件中读取一小行并将其显示在 Windows 控制台中。 我使用 MultiByteToWideChar Winapi 函数成功了: void mbtowchar(const ch
我有一个使用 MultiByteToWideChar 将 UTF-8 字符串转换为 UTF-16 的 C++ 代码片段 对于 C++,如果输入是“Hã´tel”,则输出是“Hôtel”,这是正确的 对
我想要一个将字符串转换为具有两种不同行为的宽字符串的选项: 忽略非法字符 如果出现非法字符则中止转换: 在 Windows XP 上我可以这样做: bool ignore_illegal; // in
使用 std::wstring 就像我使用 MultiByteToWideChar 一样? std::wstring widen(const std::string &in) { int le
我正在尝试编写一个可以在 EditControl 中打开和显示 ANSI 和 Unicode 的通用文本编辑器。如果我确定文本是 ANSI,是否需要重复调用 ReadFile()?无法弄清楚如何执
我正在使用一个包装 std::wstring 的类,此代码需要跨平台,是否有 Windows 函数的等价物:Linux 上的 MultiByteToWideChar 和 WideCharToMulti
使用 Win32 API MultiByteToWideChar() 从 Unicode UTF-8 转换为 Unicode UTF-16 时,是否应该使用 MB_ERR_INVALID_CHARS
我有一个函数可以将字符串从各种编码转换为 Windows 内部使用的 Unicode-16。为此,我使用了 MultiByteToWideChar应用程序接口(interface)。但我刚刚发现以下内
我是一名优秀的程序员,十分优秀!