zoukankan      html  css  js  c++  java
  • UNICODE串转换成char类型串的四种方法

     

    1. 调用 WideCharToMultiByte() API

    int WideCharToMultiByte (
        UINT    CodePage,                //1 Unicode编码的字符页,Unicode编码有字符页的概念,比如gb2312/936,big5/950等
        DWORD   dwFlags,                //2 如何处理复合unicode字符,详细查google
        LPCWSTR lpWideCharStr,        //3 待转换的unicode串
        int     cchWideChar,                //4 表示参数3的长度  传递-1表示以0x00结尾
        LPSTR   lpMultiByteStr,            //5 接受转换后的串的字符缓冲
        int     cbMultiByte,                    //6 表示参数5lpMutiByteStr的字节大小 通常sizeof一下
        LPCSTR  lpDefaultChar,        //7 NULL 具体google
        LPBOOL  lpUsedDefaultChar//8 NULL 具体google
    );

    2. 调用CRT函数wcstombs()

    size_t wcstombs (
        char*          mbstr,
        const wchar_t* wcstr,
        size_t         count );

    3. 使用CString构造器或赋值操作

    // 假设有一个Unicode串wszSomeString

    CString str1 ( wszSomeString ); // 用构造器转换
    CString str2;

    str2 = wszSomeString; // 用赋值操作转换

    4. 使用ATL串转换宏

    #include <atlconv.h>

    // 还是假设有一个Unicode串wszSomeString

    {
        char szANSIString [MAX_PATH];
        USES_CONVERSION; // 声明这个宏要使用的局部变量

        lstrcpy ( szANSIString, OLE2A(wszSomeString) );
    }

    随笔- 45  文章- 98  评论- 24 

     

    为了支持Unicode编码,需要多字节与宽字节之间的相互转换。这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读《Windows核心编程》,总结出正确的用法。
    WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
    MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
    常用的代码页由CP_ACP和CP_UTF8两个。
    使用CP_ACP代码页就实现了ANSIUnicode之间的转换。
    使用CP_UTF8代码页就实现了UTF-8Unicode之间的转换。
    下面是代码实现:
    1.  ANSI to Unicode
    wstring ANSIToUnicode( const string& str )
    {
     int  len = 0;
     len = str.length();
     int  unicodeLen = ::MultiByteToWideChar( CP_ACP,
                0,
                str.c_str(),
                -1,
                NULL,
                0 ); 
     wchar_t *  pUnicode; 
     pUnicode = new  wchar_t[unicodeLen+1]; 
     memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
     ::MultiByteToWideChar( CP_ACP,
             0,
             str.c_str(),
             -1,
             (LPWSTR)pUnicode,
             unicodeLen ); 
     wstring  rt; 
     rt = ( wchar_t* )pUnicode;
     delete  pUnicode;
     
     return  rt; 
    }
    2.  Unicode to ANSI
    string UnicodeToANSI( const wstring& str )
    {
     char*     pElementText;
     int    iTextLen;
     // wide char to multi char
     iTextLen = WideCharToMultiByte( CP_ACP,
             0,
             str.c_str(),
             -1,
             NULL,
             0,
    NULL,
             NULL );
     pElementText = new char[iTextLen + 1];
     memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
     ::WideCharToMultiByte( CP_ACP,
             0,
             str.c_str(),
             -1,
             pElementText,
             iTextLen,
             NULL,
             NULL );
     string strText;
     strText = pElementText;
     delete[] pElementText;
     return strText;
    }
    3.  UTF-8 to Unicode
    wstring UTF8ToUnicode( const string& str )
    {
     int  len = 0;
     len = str.length();
     int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,
                0,
                str.c_str(),
                -1,
                NULL,
                0 ); 
     wchar_t *  pUnicode; 
     pUnicode = new  wchar_t[unicodeLen+1]; 
     memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
     ::MultiByteToWideChar( CP_UTF8,
             0,
             str.c_str(),
             -1,
             (LPWSTR)pUnicode,
             unicodeLen ); 
     wstring  rt; 
     rt = ( wchar_t* )pUnicode;
     delete  pUnicode;
     
     return  rt; 
    }
    4.  Unicode to UTF-8   
    string UnicodeToUTF8( const wstring& str )
    {
     char*     pElementText;
     int    iTextLen;
     // wide char to multi char
     iTextLen = WideCharToMultiByte( CP_UTF8,
             0,
             str.c_str(),
             -1,
             NULL,
             0,
             NULL,
             NULL );
     pElementText = new char[iTextLen + 1];
     memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
     ::WideCharToMultiByte( CP_UTF8,
             0,
             str.c_str(),
             -1,
             pElementText,
             iTextLen,
             NULL,
             NULL );
     string strText;
     strText = pElementText;
     delete[] pElementText;
     return strText;
    }

  • 相关阅读:
    针对安卓java入门:方法的使用
    ES6里关于字符串的拓展
    ES6里关于数字的拓展
    项目笔记:导出XML和导出全部XML功能
    项目笔记:中文转拼音工具类
    深入理解dataset及其用法
    前端插件实现图片懒加载
    Java里日期转换及日期比较大小
    iframe.contentWindow 属性:关于contentWindow和contentDocument区分
    GROUP BY 和 GROUP_CONCAT的使用
  • 原文地址:https://www.cnblogs.com/liangxiaofeng/p/5765196.html
Copyright © 2011-2022 走看看