zoukankan      html  css  js  c++  java
  • 关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试

     1 #pragma once
     2 #include <stdio.h>    //getchar()
     3 #include <tchar.h>    
     4 #include <stdlib.h>  //sysytem()
     5 #include <string>    //std
     6 #include <atlstr.h>  //cstring
     7 #include <iostream>  //cout
     8 
     9 using namespace std;
    10 using std::wcout;
    11 
    12 int _tmain(int argc, _TCHAR*  argv[])
    13 {
    14     /***** char* 转换 cstring  *********/
    15     //方式一  直接赋值
    16     //char chArray[] = "This a cat!"; 
    17     // char* p = "This a cat!"; 
    18     //LPSTR p = "This a cat!"; 
    19     //CString cstr = p;
    20     //方式二 format格式转化
    21     //CString cstr1;
    22     //cstr1.Format("%s",p);
    23     //cout<< cstr1 << endl;
    24     /************     cstring转换char*     ************/
    25     //方式一(LPSTR)(LPCTSTR)强转
    26     //CString thecstring("this a cat!");
    27     //char *cp; 
    28     //*cp = (LPSTR)(LPCTSTR)thecstring;
    29     //方式二 使用strcpy
    30     //cp = new TCHAR[thecstring.GetLength() + 1];
    31     //_tcscpy_s(cp,thecstring.GetLength() + 1, thecstring);
    32     //方式三 使用CString::GetBuffer()
    33     //CString s(_T("this a cat!"));
    34     //LPTSTR cp = s.GetBuffer();
    35     //cout<< cp << endl;
    36     //if (NULL != cp)
    37     //{
    38     //    *cp = _T('');
    39         //*cp = _T('123');输出为 3his a cat  截断了字符
    40     //}
    41     //s.ReleaseBuffer();
    42 
    43     //cout<< cp << endl;
    44 
    45     /*********  WideCharToMultiByte(Unicode to Ansi) *************/
    46     wchar_t wText[20] = {L"宽字符转成窄字符!"};
    47     DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL,0,NULL,FALSE);
    48     char* psText; 
    49     psText = new char[dwNum];
    50     if (!psText)
    51     {
    52         delete []psText;
    53     }
    54     WideCharToMultiByte(CP_OEMCP, NULL, wText, -1,psText, dwNum, NULL, FALSE);
    55     
    56     cout << psText << endl;
    57     delete []psText;
    58     psText = NULL;
    59 
    60     
    61     /*********  MultiByteToWideChar(Ansi to Unicode) *************/
    62     char cText[20] = {"窄字符转成宽字符!"};
    63     DWORD dwNum1 = MultiByteToWideChar(CP_ACP, NULL,cText, -1, NULL, 0);
    64     wchar_t* pwText;
    65     pwText = new wchar_t[dwNum1];
    66     if (!pwText)
    67     {
    68         delete []pwText;
    69     }
    70     MultiByteToWideChar(CP_ACP, NULL, cText, -1, pwText, dwNum1);
    71     wchar_t wsz[] = L"ni hao a!";//宽字符和宽字符串常量前要加L
    72     //变量里存放的是中文的话,要设置区域使用wcout.imbue(locale("chs"));
    73     //才能输出变量里面的中文,不然输出的是变量地址
        //还可以设置
    全局函数setlocale(LC_ALL,"Chinese-simplified");
    74     wcout.imbue(locale("chs"));
    75     std::wcout << wsz << std::endl;
    76     std::wcout << pwText << std::endl;
    77     delete []pwText;
    78     pwText = NULL;
    79     //getchar();
    80     system("pause");
    81     return 0;
    82 }
    WideCharToMultiByte
    函数功能:该函数映射一个unicode字符串到一个多字节字符串。
    函数原型:
    int WideCharToMultiByte(
    UINT CodePage, //指定执行转换的代码页
    DWORD dwFlags, //允许你进行额外的控制,它会影响使用了读音符号(比如重音)的字符
    LPCWSTR lpWideCharStr, //指定要转换为宽字节字符串的缓冲区
    int cchWideChar, //指定由参数lpWideCharStr指向的缓冲区字符个数
    LPSTR lpMultiByteStr, //指向接收被转换字符串的缓冲区
    int cchMultiByte, //指定由参数lpMultiByteStr指向的缓冲区最大值
    LPCSTR lpDefaultChar, //遇到一个不能转换的宽字符,函数便会使用pDefaultChar参数指向的字符
    LPBOOL pfUsedDefaultChar //至少有一个字符不能转换为其多字节形式,函数就会把这个变量设为TRUE
    );
    参数:
    CodePage:指定执行转换的代码页,这个参数可以为系统已安装或有效的任何代码页所给定的值。你也可以指定其为下面的任意一值:
    CP_ACP:ANSI代码页;CP_MACCP:Macintosh代码页;CP_OEMCP:OEM代码页;
    CP_SYMBOL:符号代码页(42);CP_THREAD_ACP:当前线程ANSI代码页;
    CP_UTF7:使用UTF-7转换;CP_UTF8:使用UTF-8转换。
    函数功能:该函数映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。
    函数原型:
    int MultiByteToWideChar(
    UINT CodePage,
    DWORD dwFlags,
    LPCSTR lpMultiByteStr,
    int cchMultiByte,
    LPWSTR lpWideCharStr,
    int cchWideChar
    );
  • 相关阅读:
    [2020.11.15]CCPC Final 2019
    [2020.11.13]UOJ#424. 【集训队作业2018】count
    [2020.11.13]AtCoder Japan Alumni Group Summer Camp 2018 Day 2 K
    [2020.11.13]CF704C Black Widow
    [2020.11.13]CF765F Souvenirs
    [2020.11.13]AGC035D
    [2020.11.10]CSPS2020 翻车记
    拉格朗日反演(暂时鸽)与CF1349F2(xtq F2)
    [2020.6.20]ZJOI2020 Day1游记
    [2020.5.22]UOJ523 【美团杯2020】半前缀计数
  • 原文地址:https://www.cnblogs.com/lisuyun/p/3613543.html
Copyright © 2011-2022 走看看