转自:http://www.cnblogs.com/gakusei/articles/1585211.html

为了支持Unicode编码,需要多字节与宽字节之间的相互转换。这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读《Windows核心编程》,总结出正确的用法。
WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
下面是代码实现:
1.  ANSI to Unicode

 1 // ANSI to Unicode
 2 #include <string>
 3 using namespace std;
 4 wstring ANSIToUnicode( const string& str )
 5 {
 6     int  len = 0;
 7     len = str.length();
 8     // 返回转换后unicode的长度
 9     int  unicodeLen = ::MultiByteToWideChar(
10         CP_ACP,            // 实现ANSI与Unicode之间的转换
11         0,                //
12         str.c_str(),    // 转码前的数据
13         -1,
14         NULL,
15         0 );
16     // 申请内存
17     wchar_t *  pUnicode;
18     pUnicode = new  wchar_t[unicodeLen+1];
19     // 初始化申请的内存
20     memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
21     ::MultiByteToWideChar(
22         CP_ACP,
23         0,
24         str.c_str(),        // 转换前数据
25         -1,
26         (LPWSTR)pUnicode,    // 转换后数据
27         unicodeLen );        // 转换长度
28     wstring  rt;
29     rt = ( wchar_t* )pUnicode;
30     delete  pUnicode;        // 删除申请的内存
31
32     return  rt;
33 }

2.  Unicode to ANSI

// Unicode To ANSI
string UnicodeToANSI( const wstring& str )
{char*     pElementText;int    iTextLen;// wide char to multi chariTextLen = 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 Unicode

//UTF8 To Unicode
wstring 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

// Unicode To UTF8
string UnicodeToUTF8( const wstring& str )
{char*     pElementText;int          iTextLen;// wide char to multi chariTextLen = 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;
}

WideCharToMultiByte和MultiByteToWideChar函数的用法(转)相关推荐

  1. WideCharToMultiByte和MultiByteToWideChar函数的用法(ascii转unicode unicode转ascii)

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

  2. 关于C语言中的malloc和free函数的用法

    一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...

  3. Python assert断言函数及用法

    Python assert断言函数及用法 Python assert 语句,又称断言语句,可以看做是功能缩小版的 if 语句,它用于判断某个表达式的值,如果值为真,则程序可以继续往下执行:反之,Pyt ...

  4. python中的counter()、elements()、most_common()和subtract()函数的用法

    python中的counter().elements().most_common()和subtract()函数的用法 counter()方法: class collections.Counter([i ...

  5. Java正则表达式--Matcher.group函数的用法

    https://www.cnblogs.com/jiafuwei/p/6080984.html Java正则表达式--Matcher.group函数的用法 原来,group是针对()来说的,group ...

  6. MapInfo中常用查询函数及用法

    MapInfo中常用查询函数及用法: 函数用途 语法 备注 图层中选点 Str$(obj)="point": Str(String)表示字符串:point表示点: 图层中选线 St ...

  7. Oracle trunc()函数的用法

    --Oracle trunc()函数的用法 /**************日期********************/ 1.select trunc(sysdate) from dual  --20 ...

  8. php error log 函数,php日志函数error_log如何使用 php日志函数error_log用法介绍

    php日志函数error_log如何使用?这篇文章主要介绍了php日志函数error_log用法,结合实例形式分析了php日志函数error_log相关的配置文件设置.函数功能.用法与使用注意事项,需 ...

  9. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下  

最新文章

  1. 3D模型在网页上显示
  2. Amazon EMR(Elastic MapReduce):亚马逊Hadoop托管服务运行架构Hadoop云服务之战:微软vs.亚马逊...
  3. MATLAB生成正态样本以及正态矩阵、从文件读入矩阵
  4. 销售订单的行项目里条件的增强
  5. ITK:相互信息仿射
  6. 端口号属于协议还是进程
  7. Vue踩坑之旅(四)—— 自定义指令实现滚动加载
  8. 非常好用的轮播图插件
  9. 没有残差连接的ViT准确率只有0.15%!北大华为提出用于ViT的增强 Shortcuts,涨点显著!...
  10. 如何计算CRC循环校验码示例
  11. 考研词汇5500+地名
  12. 阿肯色大学计算机,阿肯色大学怎么样?
  13. 基于python的客流统计_基于深度学习的客流量统计方法
  14. 支付宝人脸识别身份证认证(新版SDK)
  15. 39元超值!360超级充电器拆解与评测
  16. cpufreq 频率设置verify
  17. vue获取当前时间、时间戳方法
  18. 【测试】功能测试用例设计方法总结
  19. 5G CPE 5G CPE功能优势
  20. docker mysql 高可用_docker搭建数据库高可用方案PXC

热门文章

  1. debian mysql开机启动_自动启动debian和lighttpd+mysql
  2. MySQL 笔记1 -- 安装MySQL及Navicat
  3. C++ 标准库类型 string
  4. python杂记-RSA加解密实现(4)-加解密消息及文件
  5. 工业用微型计算机(19)-指令系统(14)
  6. python源码精要(2)-C代码规范
  7. 【数据竞赛】Kaggle时序建模案例:预测水资源可用性
  8. 【机器学习】盘点Kaggle中常见的AutoML工具库及用法
  9. 【Python】刚刚,Python 3.10 正式发布了!我发现了一个可怕的功能...
  10. 【深度学习】CVPR 2021 全部论文链接公布!最新1660篇论文合集!附下载链接