多字节、UTF-8、Unicode之间的转换
// 多字节编码转为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; }
原文:http://blog.csdn.net/shellching/archive/2010/02/22/5316442.aspx
多字节、UTF-8、Unicode之间的转换相关推荐
- 多字节与UTF-8、Unicode之间的转换
from http://blog.csdn.net/frankiewang008/article/details/12832239 // 多字节编码转为UTF8编码 bool MBToUTF8(vec ...
- C++ Unicode和ANSII转换
构造字符串和转换字符串是不一样的,构造字符串时往往是添加标记,这个过程其实是告诉编译器应该怎么在内存中存储:一旦构造好,对于内存中的一块地址,这些标记符就没用了,这个时候就得使用转换函数转换了.对于C ...
- [字符集]Unicode和UTF-8之间的转换详解
最近在用VC++开发一个小工具,平时用惯了.NET,用起VC++最郁闷的就是字符串处理.当然最最让人难于琢磨的就是字符集,编码之间的转换.通过这几天的研究,终于明白了Unicode和UTF-8 ...
- Unicode和UTF-8之间的转换详解
Unicode是一个字符集,而UTF-8是 Unicode的其中一种,Unicode是定长的都为双字节,而UTF-8是可变的,对于汉字来说Unicode占有的字节比UTF-8占用的字节少1 个字节.U ...
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
参考博客: Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围 ...
- pythonunicode和str_python2 中 unicode 和 str 之间的转换及与python3 str 的区别
在python2中字符串分为 unicode 和 str 类型 Str To Unicode 使用decode(), 解码 Unicode To Str 使用encode(), 编码 返回数据给前端时 ...
- unicode和字符串之间的转换有两种方式
unicode和字符串之间的转换有两种方式. 1.1.通过JDK自带的"native2ascii"进行转换 首先,您测试的机器需要安装JDK,比如我的机器环境,我的JDK安 ...
- C++排雷:19.过滤英文和中文标点符号,string与wstring之间的转换
想要过滤一个文本中的标点符号. 对英文标点符号可以使用cctype中的ispunct方法来识别 而对于中文标点符号,则需要一定的转换: C++用string来处理字符串. string是窄字符串ASC ...
- UTF、Unicode、ASCII及中文编码
一.Unicode缘起 Unicode是一种字符编码规范 . 1.国际标准ASCII编码 先从ASCII说起.ASCII是用来表示英文字符的一种编码规范,每个ASCII字符占用1个字节(8bits) ...
最新文章
- 大数据精准投放平台_大数据库(可视化精准平台,能够使您的广告更加精准有效)...
- 全球及中国膀胱癌药物行业“十四五”专项规划及市场调研分析报告2021-2027年
- 关于华科的计算系统结构专业
- Java命令行界面(第6部分):JOpt简单
- 深入理解ArrayList
- 火狐浏览器jtopo节点切换tab后消失报错NS_ERROR_FAILURE的解决
- 为什么还有那么多人用SVN?
- logstash增量同步mysql数据到es
- css hack 记录
- cannot import name ‘Imputer‘ from ‘sklearn.preprocessing‘
- 【C语言函数调用详解】——传值调用传址调用
- 记一次PSP游戏文件(iso)提取BGM(cpk文件处理,无后缀音频文件格式转换,pmf文件转换)
- matlab导线网平差,导线网平差算例教程
- 漂亮的电脑倒计时软件_【宜收藏】博途V14软件安装教程
- 知到python课程答案-知到智慧树_Python数据分析与数据可视化_结课测验答案
- Java基础 实验四 抽象类和接口
- 国家开发银行软件测试工资待遇,国家开发银行总行工资待遇
- 万代南梦宫面向中国市场推出《太鼓达人》限量版月饼
- cypher语法 | Neo4j cypher增删查改
- 嵌入式系统 - Nand Flash 烧写问题排查
热门文章
- 水果网上销售平台_新闻和完美的推销平台
- 解决“Could not find @openzeppelin/contracts/token/ERC20/ERC20Detailed.sol“问题
- python安装pyserial
- 微信小程序调用腾讯地图API进行驾车路线规划
- Python爬虫:爬取百度图片(selenium模拟登录,详细注释)
- jmeter性能使用笔记
- springboot使用poi实现Excel模板的下载功能
- 手中无剑,心中有剑,无剑胜有剑
- 浙江师范的计算机专业的排名2015,浙江师范大学计算机科学与技术研究生专业排名...
- js判断当前设备和获取设备、浏览器宽高