先看看这篇关于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的更多相关文章

  1. 关于多字节、宽字节、WideCharToMultiByte和MultiByteToWideChar函数的详解

    所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码. 而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE. **************************** ...

  2. WideCharToMultiByte和MultiByteToWideChar函数的用法

    为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读<Windows核心编程>,总结出正确的用法. ...

  3. WideCharToMultiByte和MultiByteToWideChar函数的用法(转)

    转自:http://www.cnblogs.com/gakusei/articles/1585211.html 为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要 ...

  4. WideCharToMultiByte和MultiByteToWideChar函数的用法(转载)

    出处:http://www.cnblogs.com/gakusei/articles/1585211.html 为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要 ...

  5. C++中Cstring、wstring 和string互相转换总结

    通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...

  6. 小心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. ...

  7. Windows代码页、区域

    目录 第1章代码页    1 1 代码页    1 1.1 单字节字符集    1 1.2 双字节字符集    1 1.3 多字节字符集    1 1.4 ANSI代码页    2 2 枚举代码页   ...

  8. Windows字符集的统一与转换

    以前也零零散散看过一些字符编码的问题,今天看来这边博客,感觉很多东西都总结在里面,非常值得学习! 一.字符集的历史渊源 在Windows编程时经常会遇到编码转换的问题,一直以来让刚接触的人摸不着头脑. ...

  9. 几个字符串的误区,以及setlocale函数的使用

    转自 http://www.blogjava.net/baicker/archive/2007/08/09/135642.html 转自 http://witmax.cn/character-enco ...

随机推荐

  1. C# 应用 - 多线程 2) Thread 和 ThreadPool

    IEnumerable<int> intList = Enumerable.Range(1, 15); foreach (int i in intList) { ThreadPool.Qu ...

  2. vue+lib-flexible实现大小屏幕,超大屏幕的适配展示。

    p.p1 { margin: 0; font: 12px "PingFang SC" } span.s1 { font: 12px "Helvetica Neue&quo ...

  3. renren-fast部署发布教程(tomcat)

    renren-fast部署发布教程(tomcat) 说明:renren的开发文档需要付费,官方的生产部署介绍相对比较简单,因此记录自己的部署过程 为了方便,前后端我都部署在同一台linux服务器上,其 ...

  4. scala集合上常用的方法

    sacala 关于集合常用的操作 map1.映射:对集合中的每一个元素进行执行某一项操作2.返回值类型,正常情况不变,原来集合是什么类型,就返回什么类型3.元素类型,根据我们函数的返回值类型 val ...

  5. vue 项目集成 husky+commitlint+stylelint

    最近刚换了新工作,这两天也没有业务上的需求,做了一些前端工程化方面的东西.要在现有的项目中集成 husky+commitlint+stylelint,也不能对现有代码产生影响. 使用 lint 的目的 ...

  6. Android Studio 安装及配置

    安装时的那些事 •相关链接 [1]:无需翻墙的链接 [2]:Android Studio 安装教程 •从安装到放弃??? 初次接触 Android,并知道了开发 Android APP 的软件--An ...

  7. 软工AI Bot NABCD分析

    目标: 打造一个基于大数据的 IT 问答机器人服务,通过运用人工和AI 技术,极大提高问答产品的用户满意度. 适合高校<软件工程>,<人工智能>课程作为结对编程或者团队项目. ...

  8. 关gzip压缩,我有新发现

    1 gzip的压缩效果是立竿见影的: 2 网站是否开启gzip的查看方式 2.1 打开Chrome浏览器,按 F12打开调试面板 2.2 切换到network页签,在网络请求列表的表头,鼠标右键==& ...

  9. Tensorflow Serving 参数

    Flags: --port=8500 int32 Port to listen on for gRPC API --grpc_socket_path="" string If no ...

  10. 在Android、iOS、Web多平台使用AppGallery Connect性能管理服务

    性能管理(App Performance Management,简称APM)是华为应用市场AppGallery Connect(简称AGC)质量系列服务中的其中一项,可以提供分钟级应用性能监控能力,支 ...