tinyxml2 读取到xml文件的中文乱码处理

遇到问题:
xml文件中含有中文的文件路径,经过tinyxml2解析打印后乱码。

1、tinyxml2解析xml文件时默认是按照utf8格式读取的。

tinyxml2 读取到xml文件的中文乱码处理

2、解析含有中文的xml的乱码解决

  由1知道tinyxml2解析默认是utf8形式的,所以只要将解析后的utf8格式的字符串转为gb2312,此时再输出就不会乱码,转换函数:

#include <windows.h>

//UTF8 to GB2312  
char* U2G(const char* utf8)
{

    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);

    wchar_t* wstr = new wchar_t[len + 1];
    memset(wstr, 0, len + 1);

    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);

    char* str = new char[len + 1];
    memset(str, 0, len + 1);

    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);

    if (wstr)
    {
        delete[] wstr;
        wstr = NULL;
    }
    return str;
}


3、参考

《1》、utf8与GB2312之间的转换