zoukankan      html  css  js  c++  java
  • 转 MFC中 GB2312、UTF-8、unicode 之间转换

    //GB2312到UTF-8的转换
    static int GB2312ToUtf8(const char* gb2312, char* utf8)
    {
    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);
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return len;
    }


    //UTF-8到GB2312的转换
    static int Utf8ToGB2312(const char* utf8, char* gb2312)
    {
    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);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, gb2312, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return len;
    }


    //GB2312到Unicode的转换
    static int GB2312ToUnicode(const char* gb2312, char* unicode)
    {
    UINT nCodePage = 936; //GB2312
    int len = MultiByteToWideChar(nCodePage, 0, gb2312, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(nCodePage, 0, gb2312, -1, wstr, len);
    len = len*sizeof(wchar_t);
    memcpy(unicode, wstr, len);
    if(wstr) delete[] wstr;
    return len;
    }


    //Unicode到GB2312的转换
    static int UnicodeToGB2312(const char* unicode, int size, char*gb2312)
    {
    UINT nCodePage = 936; //GB2312
    wchar_t* wstr = new wchar_t[size/2+1];
    memcpy(wstr, unicode, size);
    int len = WideCharToMultiByte(nCodePage, 0, wstr, -1, NULL, 0, NULL, NULL);
    WideCharToMultiByte(nCodePage, 0, wstr, -1, gb2312, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return len;
    }




    //UTF-8到Unicode的转换
    static int Utf8ToUnicode(const char* utf8, char*unicode)
    {
    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);
    memcpy(unicode, wstr, len);
    if(wstr) delete[] wstr;
    return len;
    }


    //Unicode到UTF-8的转换
    static int UnicodeToUtf8(const char* unicode, int size, char* utf8)
    {
    wchar_t* wstr = new wchar_t[size/2+1];
    memcpy(wstr, unicode, size);
    int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return len;

    }

    转自http://blog.csdn.net/seven407/article/details/7712823

  • 相关阅读:
    程序员这生必须掌握的两种图形
    用一张组织架构图说清楚类和对象
    简单工厂、工厂方法、抽象工厂的比较与分析
    rabbitmq系列(一)初识rabbitmq
    【最新】经典面试100问,附答案
    使用wordPress搭建个人博客
    调试接口你还在用postman吗
    Token ,Cookie、Session傻傻分不清楚?
    你不可不知的自定义注解
    使用aop加解密http接口
  • 原文地址:https://www.cnblogs.com/endenvor/p/9965574.html
Copyright © 2011-2022 走看看