ASCII,UTF-8,Unicode字符串相互转换(转)
来源:互联网 发布:华南理工大学软件学院 编辑:程序博客网 时间:2024/06/05 02:32
[cpp] view plaincopy
- #include
- #include
- #include
- 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( "InvalidUTF-8 );sequence." -
} -
if (widesize == 0) -
{ -
throw std::exception( "Errorin );conversion." -
} -
-
std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">wchar_t resultstring(widesize); -
-
int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); -
-
if (convresult != widesize) -
{ -
throw std::exception( "Lafalla!" ); -
} -
-
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( "InvalidUTF-8 );sequence." -
} -
if (asciisize == 0) -
{ -
throw std::exception( "Errorin );conversion." -
} -
std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">char resultstring(asciisize); -
int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL); -
-
if (convresult != asciisize) -
{ -
throw std::exception( "Lafalla!" ); -
} -
-
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 char*)strascii.c_str(),= MultiByteToWideChar (CP_ACP, 0, ( -1, NULL, 0); -
if (widesize == ERROR_NO_UNICODE_TRANSLATION) -
{ -
throw std::exception( "InvalidUTF-8 );sequence." -
} -
if (widesize == 0) -
{ -
throw std::exception( "Errorin );conversion." -
} -
std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">wchar_t resultstring(widesize); -
int convresult char*)strascii.c_str(),= MultiByteToWideChar (CP_ACP, 0, ( -1, &resultstring[0], widesize); -
-
-
if (convresult != widesize) -
{ -
throw std::exception( "Lafalla!" ); -
} -
-
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( "Errorin );conversion." -
} -
-
std::vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); background-color: inherit; font-weight: bold;">char resultstring(utf8size); -
-
int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL); -
-
if (convresult != utf8size) -
{ -
throw std::exception( "Lafalla!" ); -
} -
-
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; - }
0 0
- ASCII,UTF-8,Unicode字符串相互转换(转)
- ASCII,UTF-8,Unicode字符串相互转换(转)
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- ASCII,UTF-8,Unicode字符串相互转换
- windows ASCII,UTF-8,Unicode字符串相互转换
- C++编程: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 相互转换
- Python基础---字符串编码(Unicode, UTF-8, ASCII)
- Java开发岗位面试题归类汇总八:数据库MySql
- combiner的使用误区
- 正规方程组(The normal equations)
- 蓝桥杯
- 王小二切饼
- ASCII,UTF-8,Unicode字符串相互转换(转)
- 算法课第7周第1题——516. Longest Palindromic Subsequence
- Docker与微服务
- Spring框架学习(2):基于全类名的方式配置bean
- SQL Server 跨服务器 不同数据库之间复制表的数据
- 搭建三个节点的zookeeper
- The 17th Zhejiang University Programming Contest Sponsored by TuSimple 浙大校赛C题 How Many Nines
- 进程管理(图文)
- 怎样获得程序文件名