关于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
);
相关文章推荐
- ANIS与UNICODE字符格式转换:MultiByteToWideChar() 和WideCharToMultiByte() 函数
- 关于char,wchar_t以及转化的函数mbstowcs,wcstombs,WideCharToMultiByte,MultiByteToWideChar
- 多字节转换MultiByteToWideChar和WideCharToMultiByte使用小结
- <Win32 API> 多字节字符与宽字符 WideCharToMultiByte/MultiByteToWideChar
- Wince 字符转换(WideCharToMultiByte和MultiByteToWideChar)方法
- Windows,C/C++_字符串转换_MultiByteToWideChar和WideCharToMultiByte的正确使用方法及参数详解
- MultiByteToWideChar和WideCharToMultiByte的正确使用方法及参数详解,实现字符编码转换、宽字符和字符的转换
- 用MultiByteToWideChar和WideCharToMultiByte进行短字符和宽字符之间的转换
- Linux 多字节与宽字符的相互转化(类似MultiByteToWideChar和WideCharToMultiByte)
- ANIS与UNICODE字符格式转换:MultiByteToWideChar()和WideCharToMultiByte()函数介绍
- UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)
- 用MultiByteToWideChar和WideCharToMultiByte进行短字符和宽字符之间的转换
- MultiByteToWideChar和WideCharToMultiByte用法详解, ANSI和UNICODE之间的转换
- COM初接触及字符转换WideCharToMultiByte
- TCHAR、WCHAR、CHAR以及WideCharToMultiByte、MultiByteToWideChar
- Windows中的字符转换(Multibyte to Widechar&&Widechar to Multibyte)
- WideCharToMultiByte 宽字节转换为多字节
- 关于多字节、宽字节、WideCharToMultiByte和MultiByteToWideChar函数的详解
- _tcschrs,_tcslen,WideCharToMultiByte,MultiByteToWideChar,mbstowcs,wcstombs字符串操作
- wchar*与char *相互转换 [MultiByteToWideChar、WideCharToMultiByte函数用法]