- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
使用 std::wstring
就像我使用 MultiByteToWideChar
一样?
std::wstring widen(const std::string &in)
{
int len = MultiByteToWideChar(CP_UTF8, 0, &in[0], -1, NULL, 0);
std::wstring out(len, 0);
MultiByteToWideChar(CP_UTF8, 0, &in[0], -1, &out[0], len);
return out;
}
最佳答案
如果您问它是否有效,可能是。这是对的吗?
in.c_str()
而不是 &in[0]
MultiByteToWideChar
的返回值。MultiByteToWideChar
以 (-1) 长度调用,如果成功,将包括占零终止符(即它总是返回 >= 1 成功) . std::wstring
的长度构造函数不需要这个。 std::wstring(5,0)
将为六个宽字符分配空间; 5+零项。所以从技术上讲,您分配的宽字符太多了。来自 MultiByteToWideChar
cbMultiByte
和 -1 上的文档:
If this parameter is -1, the function processes the entire input string, including the terminating null character. Therefore, the resulting Unicode string has a terminating null character, and the length returned by the function includes this character.
关于c++ - 这段代码将 wstring 与 MultiByteToWideChar 结合使用是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14184709/
我在 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)。但我刚刚发现以下内
我是一名优秀的程序员,十分优秀!