Unicode和ANSI之间转换 - U2A/A2U轻松实现
2013-04-02 14:35
316 查看
上一篇博客中描述了如何用NotePad++来实现Unicode ANSI之间的转换,这一篇中,我们将学习使用封装后的方法 U2A和A2U 来实现,具体代码如下:
#ifndef UNICODEANSI_H_ #define UNICODEANSI_H_ #define CP_JP 932 #define CP_CH 936 #define CP_KO 949 #define CP_RU 1251 #define CP_EU 1252 // ENGLISH, FRENCH, GERMAN, ITALIAN, PORTUGUESE, SPANISH class CUnicodeAnsi { public: // UNICODE -> ANSI static LPSTR U2A(LPCWSTR wStr, int iCodePage) { // Get ANSI string length. int iLen = ::WideCharToMultiByte(iCodePage, 0, wStr, -1, NULL, 0, NULL, NULL); if( 0 > iLen ) return NULL; char* aStr = new char[iLen]; // Convert Unicode to ANSI. int tmpLen = ::WideCharToMultiByte(iCodePage, 0, wStr, -1, aStr, iLen, NULL, NULL); if( 0 > tmpLen ) return NULL; return aStr; } // ANSI -> UNICODE static LPCWSTR A2U(LPCSTR aStr, int iCodePage) { // Get Unicode string length. int iLen = ::MultiByteToWideChar(iCodePage, 0, aStr, -1, NULL, 0); if( 0 > iLen ) return NULL; wchar_t* uStr = new wchar_t[iLen]; // Convert ANSI to Unicode. int tmpLen = ::MultiByteToWideChar(iCodePage, 0, aStr, -1, uStr, iLen); if( 0 > tmpLen ) return NULL; return uStr; } }; #endif // UNICODEANSI_H_
相关文章推荐
- Unicode和ANSI之间转换 - NotePad++轻松实现
- C++中Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- 我K,才发现ANSI和UNICODE之间的转换居然如此简单!!!
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- unicode和ANSI字符串之间的转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- 我K,才发现ANSI和UNICODE之间的转换居然如此简单!!!
- pb字符串实现GB2312与Unicode、UTF8之间的相互转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- MFC的中文路径的解决问题Ansi、Unicode、UTF8字符串之间的转换