zoukankan      html  css  js  c++  java
  • C++中GB2312字符串和UTF-8之间的转换

    //UTF-8到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;
    return str;
    }
    
    //GB2312到UTF-8的转换
    char* G2U(const char* gb2312)
    {
    int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
    len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len+1];
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return str;
    }
    

     有些时候,编码不正确导致中文乱码,用这个转换下编码就好了。。

    文章转载自:https://www.cnblogs.com/babietongtianta/p/3143900.html

  • 相关阅读:
    MOSS工作流开发+ Email提醒
    使用VS.net開發MOSS工作流(請假單)
    配置单一登录
    使用VS.NET手動創建一個MOSS的BDC實體
    UDDI&wsdl
    telnet
    thoughtworks~
    GIS
    MySQL中修改root密码的方法[转]
    libconfig
  • 原文地址:https://www.cnblogs.com/cyberarmy/p/10098649.html
Copyright © 2011-2022 走看看