出处: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

 wstring ANSIToUnicode( const string& str )
{
int len = ;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
,
str.c_str(),
-,
NULL,
);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+];
memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
,
str.c_str(),
-,
(LPWSTR)pUnicode,
unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete[] pUnicode; return rt;
}

2.  Unicode to ANSI

 string UnicodeToANSI( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
,
str.c_str(),
-,
NULL,
,
NULL,
NULL );
pElementText = new char[iTextLen + ];
memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
::WideCharToMultiByte( CP_ACP,
,
str.c_str(),
-,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}

3.  UTF-8 to Unicode

 wstring UTF8ToUnicode( const string& str )
{
int len = ;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
,
str.c_str(),
-,
NULL,
);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+];
memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
::MultiByteToWideChar( CP_UTF8,
,
str.c_str(),
-,
(LPWSTR)pUnicode,
unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete[] pUnicode; return rt;
}

4.  Unicode to UTF-8

 string UnicodeToUTF8( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8,
,
str.c_str(),
-,
NULL,
,
NULL,
NULL );
pElementText = new char[iTextLen + ];
memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
::WideCharToMultiByte( CP_UTF8,
,
str.c_str(),
-,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}

WideCharToMultiByte和MultiByteToWideChar函数的用法(转载)的更多相关文章

  1. WideCharToMultiByte和MultiByteToWideChar函数的用法

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

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

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

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

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

  4. linux下iconv()函数的用法(转载并修改)

    linux shell 配置文件中默认的字符集编码为UTF-8 .UTF-8是unicode的一种表达方式,gb2312是和unicode都是字符的编码方式,所以说gb2312跟utf-8的概念应该不 ...

  5. js中bind、call、apply函数的用法 (转载)

    最近看了一篇不错的有关js的文章,转载过来收藏先!!! 最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web 的项目,然后在腾讯实习的时候用 j ...

  6. R中apply等函数用法[转载]

    转自:https://www.cnblogs.com/nanhao/p/6674063.html 1.apply函数——对矩阵 功能是:Retruns a vector or array or lis ...

  7. Oracle to_date()函数的用法《转载》

    to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明, 原文地址:http://database.51cto.com/art ...

  8. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  9. [SQL]SUTFF内置函数的用法 (删除指定长度的字符并在指定的起始点插入另一组字符)

    STUFF 删除指定长度的字符并在指定的起始点插入另一组字符. 语法 STUFF ( character_expression , start , length , character_express ...

随机推荐

  1. ArcGIS API for javascript开发笔记(三)——解决打印输出的中文为乱码问题

    感谢一路走来默默支持和陪伴的你~~~ ----------------------欢迎来访,拒绝转载---------------------- 1.      调用ArcGIS API的Print实 ...

  2. python全栈开发目录

    python全栈开发目录 Linux系列 python基础 前端~HTML~CSS~JavaScript~JQuery~Vue web框架们~Django~Flask~Tornado 数据库们~MyS ...

  3. Zhu and 772002---hdu5833(高斯消元解求异或方程组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5833 题意:给n个数,选择一些数字乘积为平方数的选择方案数. 分析:每一个数字分解质因数.比如4, 6 ...

  4. 【Loadrunner】【浙江移动项目手写代码】代码备份

    vuser_init(){        lr_start_transaction("login"); web_url("10.78.224.136:8080" ...

  5. python count()

    count() 描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: str.count(sub, st ...

  6. 【剑指offer】合并两个排序的链表

    一.题目: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 二.思路: 递归思想,首先判断一方为空的情况,一方为空则直接返回另一方.都不为空时,首先找到表 ...

  7. ASP.NET一个页面的生命周期

    在学习ASP.NET页面生命周期前,需要先了解之前的ASP.NET的基本运行机制,在理解ASP.NET基本运行机制原理后,下面将介绍ASP.NET的生命周期中,页面从创建到处理结束的过程中ASP.NE ...

  8. [py]python多态-动态语言的鸭子类型

    弱类型?强类型?动态语言,静态语言 弱类型: 在程序运行过程中,类型可变 还有一种说法: 动态 variables must necessarily be defined before they ar ...

  9. [adt]python实现栈-体验数据结构

    经常使用py的一些数据结构,如list,及list的一些方法. 还有hash表等. 各类数据结构方法用的很6,然而不知道是底层是怎么实现的. 基于此,就开始研究一下py实现一些数据结构, 以便于对计算 ...

  10. Keepalived安装后出现的问题总结

    1. 在配好主从备份之后,发现虚拟IP能ping通,但是访问虚拟IP对应机器上的服务(不是apache或者mysql之类的公用软件)却不成功,这是因为要访问的服务绑定了主机上的一个实体IP不是INAD ...