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;
    }

  • 相关阅读:
    多维数组的理解
    C++ STL 中erase()的使用需要小心
    大整数相加和大整数相乘
    project1_calculator(使用tkinter实现python计算器,含有具体过程与注释)
    (转)在Source Insight中看Python代码
    class12_pack_grid_place 放置位置
    (转)AttributeError: module 'tkinter' has no attribute 'messagebox'
    class10_Frame 框架
    class9_Menubar 菜单
    class8_Canvas 画布
  • 原文地址:https://www.cnblogs.com/liangxiaofeng/p/5765196.html
Copyright © 2011-2022 走看看