您的位置:首页 > 编程语言

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

2014-03-20 13:39 609 查看
#pragma once
#include <stdio.h>    //getchar()
#include <tchar.h>
#include <stdlib.h>  //sysytem()
#include <string>    //std
#include <atlstr.h>  //cstring
#include <iostream>  //cout

using namespace std;
using std::wcout;

int _tmain(int argc, _TCHAR*  argv[])
{
/***** char* 转换 cstring  *********/
//方式一  直接赋值
//char chArray[] = "This a cat!";
// char* p = "This a cat!";
//LPSTR p = "This a cat!";
//CString cstr = p;
//方式二 format格式转化
//CString cstr1;
//cstr1.Format("%s",p);
//cout<< cstr1 << endl;
/************     cstring转换char*     ************/
//方式一(LPSTR)(LPCTSTR)强转
//CString thecstring("this a cat!");
//char *cp;
//*cp = (LPSTR)(LPCTSTR)thecstring;
//方式二 使用strcpy
//cp = new TCHAR[thecstring.GetLength() + 1];
//_tcscpy_s(cp,thecstring.GetLength() + 1, thecstring);
//方式三 使用CString::GetBuffer()
//CString s(_T("this a cat!"));
//LPTSTR cp = s.GetBuffer();
//cout<< cp << endl;
//if (NULL != cp)
//{
//    *cp = _T('\0');
//*cp = _T('123');输出为 3his a cat  截断了字符
//}
//s.ReleaseBuffer();

//cout<< cp << endl;

/*********  WideCharToMultiByte(Unicode to Ansi) *************/
wchar_t wText[20] = {L"宽字符转成窄字符!"};
DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL,0,NULL,FALSE);
char* psText;
psText = new char[dwNum];
if (!psText)
{
delete []psText;
}
WideCharToMultiByte(CP_OEMCP, NULL, wText, -1,psText, dwNum, NULL, FALSE);

cout << psText << endl;
delete []psText;
psText = NULL;

/*********  MultiByteToWideChar(Ansi to Unicode) *************/
char cText[20] = {"窄字符转成宽字符!"};
DWORD dwNum1 = MultiByteToWideChar(CP_ACP, NULL,cText, -1, NULL, 0);
wchar_t* pwText;
pwText = new wchar_t[dwNum1];
if (!pwText)
{
delete []pwText;
}
MultiByteToWideChar(CP_ACP, NULL, cText, -1, pwText, dwNum1);
wchar_t wsz[] = L"ni hao a!";//宽字符和宽字符串常量前要加L
//变量里存放的是中文的话,要设置区域使用wcout.imbue(locale("chs"));
//才能输出变量里面的中文,不然输出的是变量地址
    //还可以设置全局函数setlocale(LC_ALL,"Chinese-simplified");

 wcout.imbue(locale("chs"));
     std::wcout << wsz << std::endl;
std::wcout << pwText << std::endl;
delete []pwText;
pwText = NULL;
//getchar();
system("pause");
return 0;
}


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
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐