C++ Unicode和ANSII转换
来源:互联网 发布:制作火车票图片软件 编辑:程序博客网 时间:2024/05/19 16:23
构造字符串和转换字符串是不一样的,构造字符串时往往是添加标记,这个过程其实是告诉编译器应该怎么在内存中存储;一旦构造好,对于内存中的一块地址,这些标记符就没用了,这个时候就得使用转换函数转换了。对于C#、JAVA等语言都有Encode类,C++的库就没有那么丰富了,下面简单介绍C++比较常用的几种办法:
一、直接利用std::string和std::wstring的转换
std::string str = "I'm a string"std::wstring wstr(str.begin(), str.end());std::wstring wstr = "I'm a wstring"std::string str(wstr.begin(), wstr.end());
二、WideCharToMultiByte将unicode字符串映射到一个多字节字符串
WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
下面是代码实现:
1. ANSI to Unicodewstring ANSIToUnicode( const string& str ){ int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, NULL, 0 ); wchar_t * pUnicode; pUnicode = new wchar_t[unicodeLen+1]; memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); ::MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen ); wstring rt; rt = ( wchar_t* )pUnicode; delete pUnicode; return rt; }2. Unicode to ANSIstring UnicodeToANSI( const wstring& str ){ char* pElementText; int iTextLen; // wide char to multi char iTextLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, NULL, 0,NULL, NULL ); pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) ); ::WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL ); string strText; strText = pElementText; delete[] pElementText; return strText;}3. UTF-8 to Unicodewstring UTF8ToUnicode( const string& str ){ int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, NULL, 0 ); wchar_t * pUnicode; pUnicode = new wchar_t[unicodeLen+1]; memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen ); wstring rt; rt = ( wchar_t* )pUnicode; delete pUnicode; return rt; }4. Unicode to UTF-8 string UnicodeToUTF8( const wstring& str ){ char* pElementText; int iTextLen; // wide char to multi char iTextLen = WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL ); pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) ); ::WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL ); string strText; strText = pElementText; delete[] pElementText; return strText;}
三、使用MS的其他库
0 0
- C++ Unicode和ANSII转换
- UNICODE码到ANSII码的转换
- ANSII Escaped To Unicode 转换函数 c++ 版本
- utf8 ansii unicode三种格式的相互转换
- UTF-8,UNICODE,ANSII,GBK,GB2312相互转换
- 02-ANSII码和进制转换
- 正式博文的开篇:MTK有关字符的种种Unicode和Ansii的转化
- ansII
- [C/C++]_[utf8和unicode的相互转换]
- 文件格式从UTF8转换成ANSII格式
- 用C函数来转换Unicode和ANSI文字
- C语言实现Unicode和UTF8之间的转换
- .Net(C#)汉字和Unicode编码互相转换
- Utf8和Unicode转换问题 C语言 千字文问题
- Windows中字符集unicode,ANSII以及匈牙利命名
- 编码转换(ASCII和Unicode、Unicode和中文相互转换)
- unicode和Ascii的转换
- unicode和Ascii的转换
- ubuntu14.04 开启root登陆
- 一致性哈希概念与Python的简单实现
- 写一个操作系统内核有多难?大概的内容、步骤是什么?
- Ubuntu14.04 搭建PyQt4+Eric5 开发环境
- ZigBee 网络层概况
- C++ Unicode和ANSII转换
- learning OpenCV(2)
- C++ 控制台简单电话本实现
- 1 introduction
- java基础学习-数组
- (六) 发送函数AF_DataRequest zigbee数据发送和接受
- 卷积的解释
- Linux 查看进程
- 通信算法之十一:QPSK/DQPSK/OQPSK/BPSK/DBPSK/16QAM调制解调仿真链路