从wchar_t转换为char,反之亦然

时间:2014-01-05 16:45:35

标签: c++ encoding char wchar-t

我正在编写一个模板类String(仅用于学习目的)并且遇到一个小问题。如果T是wchar_t而U是char,反之亦然,我觉得这个方法有什么用?

template<typename U>
String<T> operator + (const U* other)
{
    String<T> newString;
    uint32_t otherLength = length(other);
    uint32_t stringLength = m_length + otherLength;
    uint32_t totalLength = stringLength * sizeof(T) + sizeof(T);

    T *buffer = new T[totalLength];

    memset(buffer, 0, totalLength);
    memcpy(buffer, m_value, m_length * sizeof(T));
    newString.m_value = buffer;
    newString.m_length = stringLength;
    memcpy(newString.m_value + m_length, other, otherLength * sizeof(T));

    return newString;
}

好的,Jared下面提出了一个解决方案,所以这样的事情(有错误,我知道,只是一个模板)?

template<typename U>
String<T> operator + (const U* other)
{
    String<T> newString;

    uint32_t sizeOfT = sizeof(T); // wchar_t is 4
    uint32_t sizeOfU = sizeof(U); // char is 1

    T* convertedString;

    int i = 0;
    while (*other != 0)
    {
        convertedString[i] = ConvertChar(*other);
        other++;
        i++;
    }

    return newString;
}

template <typename U>
T ConvertChar(U character)
{

}

2 个答案:

答案 0 :(得分:1)

现在,从U*转换为String<T>时,您的代码实际上正在使用内存副本。遗憾的是,由于wchar_tchar具有不同的内存布局,因此无法正常工作。特别是wchar_t通常占用2个字节,而char是单个byte。你需要在这里建立的是一个适当的转换函数,它应该应用于字符串

中的每个项目
T ConvertChar(U c) { ... }

答案 1 :(得分:1)

虽然您可以在从char转换为wchar_t时加宽(即使用wchar_t(c)),但它可能做错了。从wchar_t转换为char时,很明显您可能会丢失信息。实际上,个别角色实际上代表个别角色,但实际上只是代表UTF-8或UTF-16的字节。在那种情况下,可能需要将元素编码/解码成相应的其他表示。显然,转换不是一对一的:一些Unicode字符由多个UTF-8字节和多个UTF-16字组成。

您可能需要查看std::codecvt<...>以便在编码之间进行转换。

相关问题