C++编码转换函数代码
来源:互联网 发布:蚂蚁系统分类数据调用 编辑:程序博客网 时间:2024/06/06 19:52
从原文转载: http://www.2cto.com/database/201411/354891.html
//UTF-8转Unicodestd::wstring Utf82Unicode(conststd::string& utf8string){intwidesize =::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);if (widesize ==ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8sequence.");}if (widesize == 0){throw std::exception("Error inconversion.");}std::vector<wchar_t>resultstring(widesize);intconvresult =::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0],widesize);if (convresult != widesize){throw std::exception("Lafalla!");}returnstd::wstring(&resultstring[0]);}
//unicode转为 asciistring WideByte2Acsi(wstring& wstrcode){intasciisize =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);if (asciisize ==ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8sequence.");}if (asciisize == 0){throw std::exception("Error inconversion.");}std::vector<char>resultstring(asciisize);intconvresult=::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0],asciisize, NULL, NULL);if (convresult != asciisize){throw std::exception("Lafalla!");}returnstd::string(&resultstring[0]);}
//utf-8转 asciistring UTF_82ASCII(string& strUtf8Code){string strRet("");//先把 utf8 转为 unicodewstring wstr = Utf82Unicode(strUtf8Code);//最后把 unicode 转为 asciistrRet = WideByte2Acsi(wstr);returnstrRet;}
//ascii转 Unicodewstring Acsi2WideByte(string& strascii){intwidesize =MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);if (widesize ==ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8sequence.");}if (widesize == 0){throw std::exception("Error inconversion.");}std::vector<wchar_t>resultstring(widesize);intconvresult =MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1,&resultstring[0], widesize);if (convresult != widesize){throw std::exception("Lafalla!");}returnstd::wstring(&resultstring[0]);}
//Unicode转 Utf8std::string Unicode2Utf8(conststd::wstring& widestring){intutf8size =::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);if (utf8size == 0){throw std::exception("Error inconversion.");}std::vector<char>resultstring(utf8size);intconvresult =::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0],utf8size, NULL, NULL);if (convresult != utf8size){throw std::exception("Lafalla!");}returnstd::string(&resultstring[0]);}
//ascii转 Utf8string ASCII2UTF_8(string&strAsciiCode){string strRet("");//先把 ascii 转为 unicodewstring wstr = Acsi2WideByte(strAsciiCode);//最后把 unicode 转为 utf8strRet = Unicode2Utf8(wstr);returnstrRet;}
0 0
- C++编码转换函数代码
- linux c 字符编码转换函数 iconv
- 网页编码转换代码
- 代码编码批量转换
- c语言使用iconv函数实现字符编码转换
- 一套Windows上C/C++的编码转换函数
- 一套Windows上C/C++的编码转换函数
- 编码转换函数
- PHP编码转换函数
- PHP编码转换函数
- 字符编码转换函数
- PHP编码转换函数
- AS3 编码转换函数
- PHP编码转换函数
- 编码转换函数
- delphi7转换编码函数
- 编码转换函数
- 编码转换函数
- 《Android源代码系统情景分析》读书笔记 (1) Activity组件启动过程
- solution Of 1102. Invert a Binary Tree (25)
- NIO实现原理
- 【3D计算机图形学】变换矩阵、欧拉角、四元数
- 树莓派学习系列6——Sonic Pi音乐编程
- C++编码转换函数代码
- Python dis 模块初步使用
- MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案
- LeetCode--No.13--Roman to Integer
- ThreadLocal的简单入门使用
- shared_ptr的自定义析构函数的作用在于搭建一座桥
- LeetCode--No.12--Integer to Roman
- final的初始化
- Openstack组件部署 — Keystone Install & Create service entity and API endpoints