多字节与UTF-8、Unicode之间的转换
from http://blog.csdn.net/frankiewang008/article/details/12832239
- // 多字节编码转为UTF8编码
- bool MBToUTF8(vector<char>& pu8, const char* pmb, int32 mLen)
- {
- // convert an MBCS string to widechar
- int32 nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
- WCHAR* lpszW = NULL;
- try
- {
- lpszW = new WCHAR[nLen];
- }
- catch(bad_alloc &memExp)
- {
- return false;
- }
- int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen);
- if(nRtn != nLen)
- {
- delete[] lpszW;
- return false;
- }
- // convert an widechar string to utf8
- int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL);
- if (utf8Len <= 0)
- {
- return false;
- }
- pu8.resize(utf8Len);
- nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL);
- delete[] lpszW;
- if (nRtn != utf8Len)
- {
- pu8.clear();
- return false;
- }
- return true;
- }
- // UTF8编码转为多字节编码
- bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len)
- {
- // convert an UTF8 string to widechar
- int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
- WCHAR* lpszW = NULL;
- try
- {
- lpszW = new WCHAR[nLen];
- }
- catch(bad_alloc &memExp)
- {
- return false;
- }
- int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, lpszW, nLen);
- if(nRtn != nLen)
- {
- delete[] lpszW;
- return false;
- }
- // convert an widechar string to Multibyte
- int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, NULL, 0, NULL, NULL);
- if (MBLen <=0)
- {
- return false;
- }
- pmb.resize(MBLen);
- nRtn = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL);
- delete[] lpszW;
- if(nRtn != MBLen)
- {
- pmb.clear();
- return false;
- }
- return true;
- }
- // 多字节编码转为Unicode编码
- bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen)
- {
- // convert an MBCS string to widechar
- int32 uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0);
- if (uLen<=0)
- {
- return false;
- }
- pun.resize(uLen);
- int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen);
- if (nRtn != uLen)
- {
- pun.clear();
- return false;
- }
- return true;
- }
- //Unicode编码转为多字节编码
- bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen)
- {
- // convert an widechar string to Multibyte
- int32 MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL);
- if (MBLen <=0)
- {
- return false;
- }
- pmb.resize(MBLen);
- int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL);
- if(nRtn != MBLen)
- {
- pmb.clear();
- return false;
- }
- return true;
- }
- // UTF8编码转为Unicode
- bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len)
- {
- // convert an UTF8 string to widechar
- int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0);
- if (nLen <=0)
- {
- return false;
- }
- pun.resize(nLen);
- int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, &*pun.begin(), nLen);
- if(nRtn != nLen)
- {
- pun.clear();
- return false;
- }
- return true;
- }
- // Unicode编码转为UTF8
- bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen)
- {
- // convert an widechar string to utf8
- int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, NULL, 0, NULL, NULL);
- if (utf8Len<=0)
- {
- return false;
- }
- pu8.resize(utf8Len);
- int32 nRtn = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL);
- if (nRtn != utf8Len)
- {
- pu8.clear();
- return false;
- }
- return true;
- }
多字节与UTF-8、Unicode之间的转换的更多相关文章
- MultiByteToWideChar和WideCharToMultiByte用法详解, ANSI和UNICODE之间的转换
//========================================================================//TITLE:// MultiByteToW ...
- C# - 汉字与unicode之间的转换
/// <summary> /// 字符串转Unicode码 /// </summary> /// <returns>The to unicode.</ret ...
- java中unicode utf-8以及汉字之间的转换工具类
1. 汉字字符串与unicode之间的转换 1.1 stringToUnicode /** * 获取字符串的unicode编码 * 汉字"木"的Uni ...
- 举例说明Unicode 和UTF-8之间的转换
1)写这篇博客的原因 首先我要感谢这篇博客,卡了很久,看完下面这篇博客终于明白Unicode怎么转换成UTF-8了. https://blog.csdn.net/qq_32252957/article ...
- Unicode和UTF-8之间的转换
转自:http://www.cnblogs.com/xdotnet/archive/2007/11/23/unicode_and_utf8.html#undefined 最近在用VC++开发一个小工具 ...
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围:http: ...
- python中unicode, hex, bin之间的转换
python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...
- [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
最近研究搜索引擎.知识图谱和Python爬虫比较多,中文乱码问题再次浮现于眼前.虽然市面上讲述中文编码问题的文章数不胜数,同时以前我也讲述过PHP处理数据库服务器中文乱码问题,但是此处还是准备简单做下 ...
- CString-int-string-char-BSTR之间的转换
一.CString, int, string, char*之间的转换 string 转 CString CString.Format("%s", string.c_str());c ...
随机推荐
- angular用$sce服务来过滤HTML标签
angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...
- windows服务安装卸载
到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...
- 运维自动化之puppet3分钟入门
运维自动化之puppet3分钟入门 几个月前曾因为项目需求而学了点puppet的一些知识,最近因为要给别人讲一下,也就借此博文来做一下回忆,当然了,这个puppet用起来还是很不错的,尤其对我这种懒人 ...
- 用nc+简单bat/vbs脚本+winrar制作迷你远控后门
前言 某大佬某天和我聊起了nc,并且提到了nc正反向shell这个概念. 我对nc之前的了解程度仅局限于:可以侦听TCP/UDP端口,发起对应的连接. 真正的远控还没实践过,所以决定写个小后门试一试. ...
- java 四舍五入 保留两位小数
1. 格式化字符串 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); float val=Flo ...
- HDU3018 几笔画(非1笔)
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Ubuntu1804编译安装LNMP
2018-06-05 21:25:55 Ubuntu Linux GP --generic #-Ubuntu SMP Wed May :: UTC x86_64 x86_64 x86_64 GNU/L ...
- BZOJ 1083:[SCOI2005]繁忙的都市(最小生成树)
1083: [SCOI2005]繁忙的都市 Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路 ...
- POJ 2286 The Rotation Game(IDA*)
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 6396 Accepted: 21 ...
- js 如何生成一个不重复的ID的函数
在MongoDB中的ObjectID,可以理解为是一个不会重复的ID,这里有个链接http://www.jb51.net/article/101164.htm感兴趣可以去研究一下. 我今天要做的就是做 ...