使用提升语言环境

时间:2016-03-18 10:26:27

标签: c++ visual-studio gcc boost clang

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

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

char * myCharString = "0000000002" (Memory 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;
}

现在的问题是,VS2013gccclang的行为不同,即

// 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的提升实现似乎只使用输入wchar_t的最后2个字节,尽管它正确地解析了输入的开始和结束地址。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

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

因此,如果我将0x 30 00 30 00 ... 32 00存储为wchar_t,则只能按预期使用VS2013。如我所料,boost::locale将假设0x 30 00 30 00是单个字符而不是两个字符。因此,在这些平台之间产生的输出完全不同。