c++ - 使用 boost locale 将 wchar_t 转换为 char

标签 c++ visual-studio gcc boost clang

我的目标是将 wchar_t 转换为 char,我的方法是使用 boost::locale(使用 boost 1.60)。例如,

wchar_t * myWcharString = "0000000002"(内存 0x 30 00 30 00 ... 32 00)

char * myCharString = "0000000002"(内存 0x 30 30 ... 32)

我写了一个函数:

inline char* newCharFromWchar(wchar_t * utf16String) {
    char * cResult = NULL;
    try {
        std::string szResult = boost::locale::conv::from_utf(utf16String, "UTF-8");
        cResult = new char[szResult.size() + 1];
        memset(reinterpret_cast<void*>(cResult), 0, szResult.size() + 1);
        memcpy(reinterpret_cast<void*>(cResult),
           reinterpret_cast<const void*>(szResult.c_str()),
           szResult.size());
    }
    catch (...) {
        // boost::locale::conv might throw
    }
    return cResult;
}

现在的问题是 VS2013 它的行为不同于 gccclang,即

// VS 2013 behaves as expected
wchar_t * utf16String = "0000000002" (Memory 0x 30 00 30 00 ... 32 00)
char * cResult = "0000000002" (Memory 0x 30 30 ... 32)

// both gcc and clang NOT as expected:
wchar_t * utf16String = "0000000002" (Memory 0x 30 00 30 00 ... 32 00)
char * cResult = "2" (Memory 0x 32)

gccclang 的 boost 实现似乎只使用了我的输入 wchar_t 的最后 2 个字节,尽管它被正确解析了输入的开始和结束地址。

我错过了什么?

最佳答案

VS2013 将 wchar_t 作为 16 位字符,而 gcc 和 clang 都将其作为 32 位字符(在我的机器上)。

因此,如果我将 0x 30 00 30 00 ... 32 00 存储为 wchar_t,它只能按预期与 VS2013 一起使用。 boost::locale 将假定 0x 30 00 30 00 是单个字符,而不是我预期的两个字符。因此,这些平台之间的结果输出完全不同。

关于c++ - 使用 boost locale 将 wchar_t 转换为 char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081931/

相关文章:

c# - 在 Visual Studio 中安装 Directx 和 Direct3D

c - GCC 作为 m68k 交叉编译器

c++ - 什么时候使用虚拟析构函数?

c++ - 我如何知道源代码使用的是哪个版本的 OpenGL?

c++ - 有没有办法包含 std::filesystem 的转发 header ?

visual-studio - 在 VisualStudio 中将所有 *.cs 文件转换为 unicode

visual-studio - WIX 3.5 Visual Studio 项目为已安装文件的 .NET 版本创建多个 MSI

c++ - GCC 4.7 中是否存在错误。 2's implementation of shared_ptr' s(模板化)赋值运算符?

c++ - 将 GCC 中的 'delete' 警告变为错误

c# - 对于 Web 开发人员来说,学习什么好的桌面编程语言是什么?