Windows API: UTF8和UNICODE转换

网友投稿 960 2022-09-18

Windows API: UTF8和UNICODE转换

Windows API: UTF8和UNICODE转换

UTF-8与UNICODE编码格式字符串的相互转换: //UTF-8转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 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 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) { using namespace std; string strRet = ""; //先把 utf8 转为 unicode wstring wstr = Utf82Unicode(strUtf8Code); //最后把 unicode 转为 ascii strRet = WideByte2Acsi(wstr); return strRet; }

没有坚守就没有事业,没有执着就没有未来!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:IIS无法启动报错配置文件的XML格式不正确如何处理
下一篇:Python教你迅速成为蚂蚁森林排行榜第一名(Python 蚂蚁森林)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~