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

  • 相关阅读:
    无题
    晒新玩具
    PHP开发调试环境配置(基于wampserver+Eclipse for PHP Developers )
    Java
    [转]const 与 readonly知多少
    Watin 杂谈
    WCF
    [转]: 两分钟彻底让你明白Android Activity生命周期(图文)!
    【转】单链表逆序
    桥梁模式和适配器模式的区别
  • 原文地址:https://www.cnblogs.com/cyberarmy/p/10098649.html
Copyright © 2011-2022 走看看