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 }
WideCharToMultiByte 与 MultiByteToWideChar的更多相关文章
- 关于多字节、宽字节、WideCharToMultiByte和MultiByteToWideChar函数的详解
所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码. 而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE. **************************** ...
- WideCharToMultiByte和MultiByteToWideChar函数的用法
为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读<Windows核心编程>,总结出正确的用法. ...
- WideCharToMultiByte和MultiByteToWideChar函数的用法(转)
转自:http://www.cnblogs.com/gakusei/articles/1585211.html 为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要 ...
- WideCharToMultiByte和MultiByteToWideChar函数的用法(转载)
出处:http://www.cnblogs.com/gakusei/articles/1585211.html 为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要 ...
- C++中Cstring、wstring 和string互相转换总结
通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...
- 小心Windows7的UTF-8代码页
目录 第1章小心Windows7的UTF-8代码页 1 1.1 UTF-16与UTF-8相互转换 1 1.1.1 使用Windows API 1 1.1.2 自己编码 1 1. ...
- Windows代码页、区域
目录 第1章代码页 1 1 代码页 1 1.1 单字节字符集 1 1.2 双字节字符集 1 1.3 多字节字符集 1 1.4 ANSI代码页 2 2 枚举代码页 ...
- Windows字符集的统一与转换
以前也零零散散看过一些字符编码的问题,今天看来这边博客,感觉很多东西都总结在里面,非常值得学习! 一.字符集的历史渊源 在Windows编程时经常会遇到编码转换的问题,一直以来让刚接触的人摸不着头脑. ...
- 几个字符串的误区,以及setlocale函数的使用
转自 http://www.blogjava.net/baicker/archive/2007/08/09/135642.html 转自 http://witmax.cn/character-enco ...
随机推荐
- MySQL 40题练习题和答案
2.查询"生物"课程比"物理"课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号, ...
- Linux Shell 统计一(行\列)数值的总和及行、列转换
(对一列数字求和) 在日常工作当中需要对文本过滤出来的数字进行求和运算,例如想统计一个MySQL分区表现在有多大 # ls -lsh AdPlateform#P#p*.ibd |grep G 2.6 ...
- 常用开发库 - 告別BeanUtils拷贝,MapStruct工具库最全详解
常用开发库 - MapStruct工具库详解 MapStruct是一款非常实用Java工具,主要用于解决对象之间的拷贝问题,比如PO/DTO/VO/QueryParam之间的转换问题.区别于BeanU ...
- 多种细分方式浏览销售数据,IAP助您有效洞察市场收益效果
华为应用内支付服务是直接在应用程序内提供购买商品或订阅等功能,为了能够让开发者更好的了解应用内的销售额及商品购买.订阅的市场收益效果,华为应用内支付服务提供的消费数据统计和数据报表,支持多种细分方式浏 ...
- pwnable.kr第三题bof
Running at : nc pwnable.kr 9000 IDA查看 1 unsigned int __cdecl func(int a1) 2 { 3 char s; // [esp+1Ch] ...
- golang 性能调优分析工具 pprof(下)
golang 性能调优分析工具 pprof(上)篇, 这是下篇. 四.net/http/pprof 4.1 代码例子 1 go version go1.13.9 把上面的程序例子稍微改动下,命名为 d ...
- 生产中常用的获取IP地址方法的总结
从ifconfig命令的结果中筛选出除了lo网卡之外的所有IPv4地址 centos7 (1)ifconfig | awk '/inet / && !($2 ~ /^127/){pri ...
- 【RocketMQ源码分析】深入消息存储(2)
前文回顾 CommitLog篇 --[RocketMQ源码分析]深入消息存储(1) MappedFile篇 --[RocketMQ源码分析]深入消息存储(3) 前文说完了一条消息如何被持久化到本地磁盘 ...
- [Fundamental of Power Electronics]-PART I-6.变换器电路-0 序
6 变换器电路 我们已经分析了包括buck,boost,buck-boost以及cuk电路,电压源逆变器等一系列电路的工作原理.利用这些变换器,可以执行许多不同的功能:降压,升压,极性反转以及直流交流 ...
- Java字节流和字符流,是时候总结一下IO流了
目录 从接收输入值说起 字节流读取 字符流读取 Scanner 读取 什么是 IO 流 字节流和字符流 字节流 字节输入流 字节输出流 缓冲流的原理 字符流 字符输入流 字符输出流 为什么字符流需要 ...