zoukankan      html  css  js  c++  java
  • C++ UTF8和GB2312相互转换

    #include <Windows.h>
    #include <string>
    using std::string;
    
    void UTF8_to_GB2312(const char* utf8, string& gb2312_str)
    {
        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);
        delete[] wstr;
        gb2312_str = str;
        delete[] str;
        return;
    }
    
    void GB2312_to_UTF8(const char* gb2312, string& utf8_str)
    {
        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);
        delete[] wstr;
        utf8_str = str;
        delete[] str;
        return;
    }
  • 相关阅读:
    第二次作业
    第一次软件测试
    activity带数据跳转
    学生注册界面
    activity_main
    购物商城
    2021.4.14JSP第七周作业
    2021.4.7jsp第六周作业
    2021.3.26JSP第四次作业
    2021/3/10 第二次JSP作业
  • 原文地址:https://www.cnblogs.com/mzhrd/p/4759390.html
Copyright © 2011-2022 走看看