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 ...
随机推荐
- Apache配置 7.静态元素过期时间
(1)介绍 那到底能缓存多久呢?如果服务器上的某个图片更改了,那么应该访问新的图片才对.这就涉及一个静态文件缓存时长的问题,也叫作"缓存过期时间".在httpd的配置文件中,我们是 ...
- 为 .NET 打 Call,为国产平台 Gitee 打 Call,我的 .NET/C# 开源项目清单,同步维护于 Github 和 Gitee
所有项目遵循 MIT 开源协议.可以随意使用,但是需在源代码和产品关于画面保留版权声明和我的网站链接,谢谢. Sheng.Winform.IDE Github:https://github.com/i ...
- 攻防世界 reverse leaked-license-64
mark一下,以后分析 原文:http://sibears.ru/labs/ASIS-CTF-Quals-2016-Leaked_License/ [ASIS CTF Quals 2016] - 泄露 ...
- Android Studio 之 编写精美的聊天界面
•准备工作 首先制作一张 .9 格式的聊天气泡,参见我的这篇博客: 需要注意的是,制作完成后,应该将原始文件删除,否则AS会分不清楚而报错. 新建一个 Empty Activity,Java 和 XM ...
- go的令牌桶实现库 go-rate
关于我 我的博客|文章首发 go-rate是速率限制器库,基于 Token Bucket(令牌桶)算法实现. go-rate被用在LangTrend的生产中 用于遵守GitHub API速率限制. 速 ...
- 总结traefik 在k8s 环境中的配置文件
总结traefik 在k8s 环境中的配置文件 traefik.toml配置文件引用 [www@localhost traefik-ingress]$ more * :::::::::::::: co ...
- 使用Docker Toolbox 创建Docker虚拟机的方法-注意正确使用本地文件 file:参数的路径名
使用Docker Toolbox 创建v1.12.6版的Docker虚拟机的方法, 一定要注意正确使用本地文件 file:// 参数的路径名, 之前尝试创建过多次,一直都没有成功过, 无法使用 fil ...
- Salesforce学习之路(七)Visualforce结合Reports展示图表
Salesforce作为一款CRM系统,个人觉得最重要的环境便是在于数据的展示和联动,而Salesforce也本身提供了相当强大的功能,Report在展示图表的方面十分强大,前段时间更是宣布以157亿 ...
- Day12_60_多线程的创建和启动(一)
多线程的创建和启动 * 在java语言中实现多线程的第一种方式, 继承 java.lang.Thread; 之后重写run()方法. * 使用多线程之后,主线程和其他线程是不在同一个栈中的,一个线程对 ...
- 【秒懂音视频开发】18_详解YUV
本文的主角是多媒体领域非常重要的一个概念:YUV. 简介 YUV,是一种颜色编码方法,跟RGB是同一个级别的概念,广泛应用于多媒体领域中. 也就是说,图像中每1个像素的颜色信息,除了可以用RGB的方式 ...