{"id":2053,"date":"2023-04-01T21:19:02","date_gmt":"2023-04-01T13:19:02","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2053"},"modified":"2023-04-07T06:06:13","modified_gmt":"2023-04-06T22:06:13","slug":"cpp-ansi-utf-8-and-unicode-transcoding","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/cpp-ansi-utf-8-and-unicode-transcoding\/","title":{"rendered":"C++ ANSI\u53caUTF-8\u4e0eUnicode\u8f6c\u7801"},"content":{"rendered":"<h2>UTF-8\u4e0eUnicode\u8f6c\u7801<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;codecvt&gt;\n\nstd::string UnicodeToUTF8(const std::wstring &amp; wstr)\n{\n    std::string ret;\n    try {\n        std::wstring_convert&lt; std::codecvt_utf8&lt;wchar_t&gt; &gt; wcv;\n        ret = wcv.to_bytes(wstr);\n    } catch (const std::exception &amp; e) {\n        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n    return ret;\n}\n\nstd::wstring UTF8ToUnicode(const std::string &amp; str)\n{\n    std::wstring ret;\n    try {\n        std::wstring_convert&lt; std::codecvt_utf8&lt;wchar_t&gt; &gt; wcv;\n        ret = wcv.from_bytes(str);\n    } catch (const std::exception &amp; e) {\n        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n    return ret;\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>ANSI\u4e0eUnicode\u8f6c\u7801<\/h2>\n<pre><code class=\"language-cpp\">std::string UnicodeToANSI(const std::wstring &amp;wstr) {\n    std::string ret;\n    std::mbstate_t state = {};\n    const wchar_t *src = wstr.data();\n    size_t len = std::wcsrtombs(nullptr, &amp;src, 0, &amp;state);\n    if (static_cast&lt;size_t&gt;(-1) != len) {\n        std::unique_ptr&lt;char[]&gt; buff(new char[len + 1]);\n        len = std::wcsrtombs(buff.get(), &amp;src, len, &amp;state);\n        if (static_cast&lt;size_t&gt;(-1) != len) {\n            ret.assign(buff.get(), len);\n        }\n    }\n    return ret;\n}\n\nstd::wstring ANSIToUnicode(const std::string &amp;str) {\n    std::wstring ret;\n    std::mbstate_t state = {};\n    const char *src = str.data();\n\n    size_t len = std::mbsrtowcs(nullptr, &amp;src, 0, &amp;state);\n    if (static_cast&lt;size_t&gt;(-1) != len) {\n        std::unique_ptr&lt;wchar_t[]&gt; buff(new wchar_t[len + 1]);\n        len = std::mbsrtowcs(buff.get(), &amp;src, len, &amp;state);\n        if (static_cast&lt;size_t&gt;(-1) != len) {\n            ret.assign(buff.get(), len);\n        }\n    }\n    return ret;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>UTF-8\u4e0eUnicode\u8f6c\u7801 #include &lt;iostream&gt; #include &lt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[517],"tags":[],"class_list":["post-2053","post","type-post","status-publish","format-standard","hentry","category-c-cpp"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2053"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2053\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}