转自: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

 // ANSI to Unicode
#include <string>
using namespace std;
wstring ANSIToUnicode( const string& str )
{
int len = ;
len = str.length();
// 返回转换后unicode的长度
int unicodeLen = ::MultiByteToWideChar(
CP_ACP, // 实现ANSI与Unicode之间的转换
, //
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

// 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

//UTF8 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

// Unicode To UTF8
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. 有关日期的函数操作用法总结,to_date(),trunc(),add_months();

    相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...

  5. Oracle to_date()函数的用法

    Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...

  6. js中bind、call、apply函数的用法

    最近一直在用 js 写游戏服务器,我也接触 js 时间不长,大学的时候用 js 做过一个 H3C 的 web的项目,然后在腾讯实习的时候用 js 写过一些奇怪的程序,自己也用 js 写过几个的网站.但 ...

  7. Oracle trunc()函数的用法

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

  8. freemarker内置函数和用法

    原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...

  9. matlab中patch函数的用法

    http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...

随机推荐

  1. POJ 3207 Ikki's Story IV - Panda's Trick (2-sat)

    Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 6691   ...

  2. 【java】详解集合

    目录结构: contents structure [-] 集合概述 什么是集合 Collection和Map的区别 List和Set的区别 ArrayList和LinkedList的区别 HashSe ...

  3. iOS 图标

    iOS icon是一件很头疼的事情 大致多少张呢,忘记了,下面开发者中心给的一个文档,自己捋捋有多少张 180934.jpg 幸亏不是自己画的,不然要骂姥姥,但是多数的UI是妹子啊,让人家做人家会说: ...

  4. C语言中的 (void*)0 与 (void)0

    前几天看到一个宏, 它大概是这样的: #define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LI ...

  5. J2EEweb开发中的缓存问题的研究

    一般情况下,浏览器都会缓存已经访问过的页面内容,关于如何禁止浏览器缓存的介绍,在网上到处都有相关的文章,但是,关于浏览器如何利用缓存,如何处理缓存的讲解,却鲜有人谈及 .浏览器在访问已缓存过的资源时, ...

  6. 【转载】linux 测试机器端口连通性方法

    转载原文:http://blog.csdn.net/z1134145881/article/details/54706711 下面一一介绍: 1 telnet方法 2 wget方法 3 ssh方法 4 ...

  7. IRGAN:A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models

    https://arxiv.org/pdf/1705.10513.pdf 论文阅读笔记: https://www.cnblogs.com/liaohuiqiang/p/9694277.html htt ...

  8. C#基础第二天-作业答案-九九乘法表-打印星星

    题一:九九乘法表的答案 //正三角 ; i < ; i++) { ; j <= i; j++) { Console.Write("{0}*{1}={2} ", j, i ...

  9. java 执行mysql 8.0.11存储过程报错The user specified as a definer ('root'@'10.%.%.%') does not exist解决办法

    执行存储过程,报错 java.sql.SQLException: The user specified as a definer ('root'@'10.%.%.%') does not exist ...

  10. jmeter运行时间越久发送请求越来越少

    displayed. Size: 412152 > 204800,而且每次点击查看“察看结果树”后会导致jmeter卡死, 解决方法: step1.在user.property中增加 view. ...