先看看这篇关于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. python 操作符** (两个乘号就是乘方)

    一个乘号*,如果操作数是两个数字,就是这两个数字相乘,如2*4,结果为8**两个乘号就是乘方.比如3**4,结果就是3的4次方,结果是81 *如果是字符串.列表.元组与一个整数N相乘,返回一个其所有元 ...

  2. JAVA安装第一步JDK

    安装JDK----(一学就会) 一.百度搜索JDK,找到下载的地址 二.下载属于自己电脑的对应版本 三.下载到本地之后,双击安装JDK 四.配置环境变量 我的电脑->右键->属性 环境变量 ...

  3. 精通模块化JavaScript

    近日读了一本名为<精通模块化JavaScript>的书,并记录了其中的精髓. 一.模块化思维 精通模块化开发并不是指要遵循一套定义明确的规则,而是指能够将自己置身于使用者的角度,为可能即将 ...

  4. 时间同步chrony,最全最细

    时间同步服务 多主机协作工作时,各个主机的时间同步很重要,时间不一致会造成很多重要应用的故障,如:加密协 议,日志,集群等, 利用NTP(Network Time Protocol) 协议使网络中的各 ...

  5. pycharm在debug时总是报UnicodeDecodeError

    1,原文链接 解决pycharm run 正常 debug 报 UnicodeDecodeError 错误的问题 2,解决方法 首先尝试 如果上面还不行

  6. java例题_06 最大公约数&最小公倍数

    1 /*6 [程序 6 求最大公约数及最小公倍数] 2 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数. 3 程序分析:利用辗除法. 4 */ 5 6 /*分析 7 * ======== ...

  7. 使用 Android Studio 开发 widget 安卓桌面插件

    •What AppWidget 即桌面小部件,也叫桌面控件,就是能直接显示在Android系统桌面上的小程序: 这么说可能有点抽象,看图: 像这种,桌面上的天气.时钟.搜索框等等,都属于 APP Wi ...

  8. Spring的循环依赖

    本文简要介绍了循环依赖以及Spring解决循环依赖的过程 一.定义 循环依赖是指对象之间的循环依赖,即2个或以上的对象互相持有对方,最终形成闭环.这里的对象特指单例对象. 二.表现形式 对象之间的循环 ...

  9. 结对作业-stage_1

    教学班 罗杰.任建班周五3.4节 gitlab项目地址 Here it is. 成员 周远航(3004) 李辰洋(3477) 结对编程体验 感受 在前期设计时,两人合作可以收集更多资料,提供更多想法, ...

  10. 记一次phpwind的漏洞测试学习

    实验:phpwind的文件目录遍历 工具:windows2003,Windows10,phpstudy2018,phpwind8.7 在Windows2003中,安装phpstudy并且部署phpwi ...