ASCII,UTF-8,Unicode字符串相互转换
来源:互联网 发布:oracle调用java web 编辑:程序博客网 时间:2024/05/18 00:53
[cpp] view plaincopyprint?
- #include<string>
- #include<windows.h>
- #include<vector>
- using namespace std;
- //utf8 转 Unicode
- std::wstring Utf82Unicode(const std::string& utf8string)
- {
- int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
- if (widesize == ERROR_NO_UNICODE_TRANSLATION)
- {
- throw std::exception("Invalid UTF-8 sequence.");
- }
- if (widesize == 0)
- {
- throw std::exception("Error in conversion.");
- }
- std::vector<wchar_t> resultstring(widesize);
- int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
- if (convresult != widesize)
- {
- throw std::exception("La falla!");
- }
- return std::wstring(&resultstring[0]);
- }
- //unicode 转为 ascii
- std::string WideByte2Acsi(std::wstring& wstrcode)
- {
- int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
- if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
- {
- throw std::exception("Invalid UTF-8 sequence.");
- }
- if (asciisize == 0)
- {
- throw std::exception("Error in conversion.");
- }
- std::vector<char> resultstring(asciisize);
- int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
- if (convresult != asciisize)
- {
- throw std::exception("La falla!");
- }
- return std::string(&resultstring[0]);
- }
- //utf-8 转 ascii
- std::string UTF_82ASCII(std::string& strUtf8Code)
- {
- std::string strRet("");
- //先把 utf8 转为 unicode
- std::wstring wstr = Utf82Unicode(strUtf8Code);
- //最后把 unicode 转为 ascii
- strRet = WideByte2Acsi(wstr);
- return strRet;
- }
- ///////////////////////////////////////////////////////////////////////
- //ascii 转 Unicode
- std::wstring Acsi2WideByte(std::string& strascii)
- {
- int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
- if (widesize == ERROR_NO_UNICODE_TRANSLATION)
- {
- throw std::exception("Invalid UTF-8 sequence.");
- }
- if (widesize == 0)
- {
- throw std::exception("Error in conversion.");
- }
- std::vector<wchar_t> resultstring(widesize);
- int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
- if (convresult != widesize)
- {
- throw std::exception("La falla!");
- }
- return std::wstring(&resultstring[0]);
- }
- //Unicode 转 Utf8
- std::string Unicode2Utf8(const std::wstring& widestring)
- {
- int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
- if (utf8size == 0)
- {
- throw std::exception("Error in conversion.");
- }
- std::vector<char> resultstring(utf8size);
- int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
- if (convresult != utf8size)
- {
- throw std::exception("La falla!");
- }
- return std::string(&resultstring[0]);
- }
- //ascii 转 Utf8
- std::string ASCII2UTF_8(std::string& strAsciiCode)
- {
- std::string strRet("");
- //先把 ascii 转为 unicode
- std::wstring wstr = Acsi2WideByte(strAsciiCode);
- //最后把 unicode 转为 utf8
- strRet = Unicode2Utf8(wstr);
- return strRet;
- }
#include<string>#include<windows.h>#include<vector>using namespace std;//utf8 转 Unicodestd::wstring Utf82Unicode(const std::string& utf8string){int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);if (widesize == ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8 sequence."); }if (widesize == 0){throw std::exception("Error in conversion.");} std::vector<wchar_t> resultstring(widesize); int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); if (convresult != widesize){throw std::exception("La falla!");} return std::wstring(&resultstring[0]);}//unicode 转为 asciistd::string WideByte2Acsi(std::wstring& wstrcode){int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);if (asciisize == ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8 sequence.");}if (asciisize == 0){throw std::exception("Error in conversion.");} std::vector<char> resultstring(asciisize);int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL); if (convresult != asciisize){throw std::exception("La falla!");} return std::string(&resultstring[0]);} //utf-8 转 asciistd::string UTF_82ASCII(std::string& strUtf8Code){std::string strRet("");//先把 utf8 转为 unicodestd::wstring wstr = Utf82Unicode(strUtf8Code);//最后把 unicode 转为 asciistrRet = WideByte2Acsi(wstr);return strRet;}/////////////////////////////////////////////////////////////////////////ascii 转 Unicodestd::wstring Acsi2WideByte(std::string& strascii){int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);if (widesize == ERROR_NO_UNICODE_TRANSLATION){throw std::exception("Invalid UTF-8 sequence.");}if (widesize == 0){throw std::exception("Error in conversion.");} std::vector<wchar_t> resultstring(widesize);int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);if (convresult != widesize){throw std::exception("La falla!");} return std::wstring(&resultstring[0]);}//Unicode 转 Utf8std::string Unicode2Utf8(const std::wstring& widestring){int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);if (utf8size == 0){throw std::exception("Error in conversion.");} std::vector<char> resultstring(utf8size); int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL); if (convresult != utf8size){throw std::exception("La falla!");} return std::string(&resultstring[0]);}//ascii 转 Utf8std::string ASCII2UTF_8(std::string& strAsciiCode){std::string strRet("");//先把 ascii 转为 unicodestd::wstring wstr = Acsi2WideByte(strAsciiCode);//最后把 unicode 转为 utf8strRet = Unicode2Utf8(wstr);return strRet;}
0 0
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- windows ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换(转)
- C++编程:ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换(转)
- utf,unicode,ascii字符串之间的相互转换
- 字符串UTF-8与ASCII相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- Python的ASCII, GB2312, Unicode , UTF-8 相互转换
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- 字符串编码格式相互转换Unicode --UTF-8 -- GBK
- Python的ASCII, UTF-8 相互转换
- 字符串编码ASCII、Unicode和UTF-8
- 一个按钮执行多个事件
- iOS根据字符的大小固定宽度返回高度
- 20141226 N2
- 天数计算器 JS
- 全功能的Java剖析工具(profiler)
- ASCII,UTF-8,Unicode字符串相互转换
- 展览设计如何凸显特色
- LINUX环境下MYSQL5.5源码安装
- STL中的set容器的一点总结在set中每个元素的值都唯一
- linode的网卡丢失问题解决了(终极解法)
- boost::condition的使用
- Plus One (Java)
- Django POST CSRF verification failed. Request abort
- win10如何设置自己喜欢图片为桌面背景