出处: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. Taking a peek inside with the Actuator

    Taking a peek inside with the Actuator <dependency> <groupId>org.springframework.boot< ...

  2. iOS-Foundation框架—结构体(转载)

    一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...

  3. SQL SERVER深入学习学习资料参考

    SQL SERVER深入学习学习资料参考 1.微软Webcast<sql server 2000完结篇>. 尽管微软Webcast出了很多关于Sql Server的系列课程,但是最为深入讲 ...

  4. zabbix 自定义监控项 获取nginx监控状态

    本篇是基于zabbix 源码安装的 [root@localhost conf]# curl "http://192.168.0.73:8080/nginx_status" Acti ...

  5. [py]py常用模块小结

    - python md5校验: https://blog.csdn.net/linda1000/article/details/17581035 import hashlib hashlib.md5( ...

  6. selenium python 启动Chrome

    启动Chrom浏览器 下载chromedriver: http://chromedriver.storage.googleapis.com/index.html 当时找chromedriver与chr ...

  7. iOS 界面启动时,功能新特征显示

    APP新启动的时候,都会有几张新的图片滑动,才能到主的界面.现在,我们新建一个控制器,专门来处理新特性,直接上代码. 第一步:新建一个NewfeatureController // // HWNewf ...

  8. 7.6 Models -- Finding Records

    Ember Data的store为检索一个类型的records提供一个接口. 一.Retrieving a single record(检索单记录) 1. 通过type和ID使用store.findR ...

  9. Android弹性滑动的三种实现方式

    引言 上一篇文章我们介绍了实现弹性滑动的三种方式,但仅仅是给出了代码片段和方法理论.今天我们结合一个具体的例子来谈一下如何使用这三种方法来实现弹性滑动.今天我们的例子是仿IOS的下拉操作,我们知道An ...

  10. [转载]C#深拷贝的方法

    首先了解下深拷贝和浅拷贝的定义: 浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用.      深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对 ...