首页 > 其他 > 详细

关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与窄字符的转换)以及字符串的转换代码测试

时间:2014-03-20 14:05:12      阅读:423      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 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(‘\0‘);
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 }
bubuko.com,布布扣

 

关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与窄字符的转换)以及字符串的转换代码测试,布布扣,bubuko.com

关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与窄字符的转换)以及字符串的转换代码测试

原文:http://www.cnblogs.com/lisuyun/p/3613543.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!