多字节、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) ...
最新文章
- python使用imbalanced-learn的RepeatedEditedNearestNeighbours方法进行下采样处理数据不平衡问题
- 2021年北京高校数学建模校际联赛题目_B
- 【企业管理】2020年3-4 月 每日花语
- mysql definer_mysql常见问题之视图权限控制--安全性为DEFINER
- linux中使用随机数
- sqlite 数据量_向SQLite批量导入csv,txt数据
- linux 字符设备号分配状况
- Linux工作笔记037---Centos下Linux创建用户_用户组_删除用户
- 表格送货单自动编号vba_制作仓库入库单,自动登记商品数据,这段代码只需三步搞定...
- Java常用的框架有哪些?
- autoload.php beanbun_PHP爬虫框架Beanbun使用
- 3dmax一键展uv_3Dmax批量展开场景物体的第二套UV
- 学习记录:小程序图片上传至服务器
- Windows 中剪贴板的操作
- Python3使用dbf模块读写dbf文件
- C++基础笔记——汇总版(上)
- Q1净亏损同比扩大222% 四通一达业绩垫底百世还能逆袭吗?
- csv逗号分割不兼容 解决_excel保存为csv 不兼容的功能
- 算式最大值 (思维题)
- 读取pb模型进行预测
热门文章
- 用python画小仓鼠教程_彩铅画教程:教你画小仓鼠
- 游戏静态HTML网页作业作品 大学生游戏介绍网页设计制作成品 简单DIV CSS布局网站
- 超融合一体机与一体机的区别
- Interacting Attention Graph for Single Image Two-Hand Reconstruction(单幅图像双手重建的交互注意图)
- 【Math ML】Lagrange Multipliers 拉格朗日乘数
- 在线加密解密工具地址 https://www.keylala.cn
- 计算机基础(04)操作系统基础
- 兰州大学本科生发表31篇论文引关注!本人及校方回应
- 速率法和终点法的区别_生化反应曲线解析1(终点法)
- 实现 RSA 算法之改进和优化(第三章)(老物)