WideCharToMultiByte和MultiByteToWideChar函数的用法 (转)
来源:互联网 发布:上古5白雪sg捏脸数据 编辑:程序博客网 时间:2024/05/17 14:21
WideCharToMultiByte和MultiByteToWideChar函数的用法 (转)
先看看这篇关于Windows编码的文章:http://blog.csdn.net/shyboy_nwpu/article/details/4431668
再看看这篇关于两个函数参数和用法的说明:http://www.cnblogs.com/wind-net/archive/2012/10/10/2718340.html
为了支持Unicode编码,需要多字节与宽字节之间的相互转换。这两个系统函数在使用时需要指定代码页。
WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个:
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
1. ANSI to Unicode
1 wstring ANSIToUnicode( const string& str ) 2 { 3 int len = 0; 4 len = str.length(); 5 int unicodeLen = ::MultiByteToWideChar( CP_ACP, 6 0, 7 str.c_str(), 8 -1, 9 NULL,10 0 ); 11 wchar_t * pUnicode; 12 pUnicode = new wchar_t[unicodeLen+1]; 13 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 14 ::MultiByteToWideChar( CP_ACP,15 0,16 str.c_str(),17 -1,18 (LPWSTR)pUnicode,19 unicodeLen ); 20 wstring rt; 21 rt = ( wchar_t* )pUnicode;22 delete pUnicode; 23 return rt; 24 }
2. Unicode to ANSI
1 string UnicodeToANSI( const wstring& str ) 2 { 3 char* pElementText; 4 int iTextLen; 5 // wide char to multi char 6 iTextLen = WideCharToMultiByte( CP_ACP, 7 0, 8 str.c_str(), 9 -1,10 NULL,11 0,12 NULL,13 NULL );14 pElementText = new char[iTextLen + 1];15 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );16 ::WideCharToMultiByte( CP_ACP,17 0,18 str.c_str(),19 -1,20 pElementText,21 iTextLen,22 NULL,23 NULL );24 string strText;25 strText = pElementText;26 delete[] pElementText;27 return strText;28 }
3. UTF-8 to Unicode
1 wstring UTF8ToUnicode( const string& str ) 2 { 3 int len = 0; 4 len = str.length(); 5 int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 6 0, 7 str.c_str(), 8 -1, 9 NULL,10 0 ); 11 wchar_t * pUnicode; 12 pUnicode = new wchar_t[unicodeLen+1]; 13 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 14 ::MultiByteToWideChar( CP_UTF8,15 0,16 str.c_str(),17 -1,18 (LPWSTR)pUnicode,19 unicodeLen ); 20 wstring rt; 21 rt = ( wchar_t* )pUnicode;22 delete pUnicode; 23 return rt; 24 }
4. Unicode to UTF-8
1 string UnicodeToUTF8( const wstring& str ) 2 { 3 char* pElementText; 4 int iTextLen; 5 // wide char to multi char 6 iTextLen = WideCharToMultiByte( CP_UTF8, 7 0, 8 str.c_str(), 9 -1,10 NULL,11 0,12 NULL,13 NULL );14 pElementText = new char[iTextLen + 1];15 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );16 ::WideCharToMultiByte( CP_UTF8,17 0,18 str.c_str(),19 -1,20 pElementText,21 iTextLen,22 NULL,23 NULL );24 string strText;25 strText = pElementText;26 delete[] pElementText;27 return strText;28 29 }
0 0
- WideCharToMultiByte和MultiByteToWideChar函数的用法 (转)
- WideCharToMultiByte和MultiByteToWideChar函数的用法 (转)
- WIDECHARTOMULTIBYTE和MULTIBYTETOWIDECHAR函数的用法 (转)
- WideCharToMultiByte和MultiByteToWideChar函数的用法 (转)
- WIDECHARTOMULTIBYTE和MULTIBYTETOWIDECHAR函数的用法 (转)
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- (ZT)WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- WideCharToMultiByte和MultiByteToWideChar函数的用法
- makefile---使用共享库文件so
- OCP-V13-707
- 从全部控件中找指定类型的控件
- 更新xcode7运行程序,报错*** Assertion failure in -[UIApplication _runWithMainScene:transi
- Android Sdk 快速下载,解决下载慢的问题
- WideCharToMultiByte和MultiByteToWideChar函数的用法 (转)
- Codeforces #313 (Div. 1) C. Gerald and Giant Chess dp 组合数 逆元
- Vijos 题目1470 教主的后花园(DP)
- Android 手机上获取物理唯一标识码
- /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found
- 【VBS脚本教程】:写入记事本文件及输出
- leetcode-110-Balanced Binary Tree
- 《问题与总结》
- Linux下so文件的生成和使用