{"id":486,"date":"2014-06-05T18:16:44","date_gmt":"2014-06-05T22:16:44","guid":{"rendered":"http:\/\/jaewon.hwang.info\/jaewon\/?p=486"},"modified":"2014-06-06T01:37:24","modified_gmt":"2014-06-06T05:37:24","slug":"conversion-between-utf-16-and-utf-8-in-c","status":"publish","type":"post","link":"https:\/\/jaewon.hwang.info\/jaewon\/2014\/06\/conversion-between-utf-16-and-utf-8-in-c\/","title":{"rendered":"Conversion between UTF-16 and UTF-8 in C++"},"content":{"rendered":"<pre class=\"brush: cpp; toolbar: false;\">\r\n#include &lt;fstream&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;locale&gt;\r\n#include &lt;codecvt&gt;\r\n\r\n\/\/ Unicode representation in MS Windows uses the 2-byte wchar_t type.\r\nstd::wstring_convert&lt;std::codecvt_utf8_utf16&lt;wchar_t&gt;, wchar_t&gt; utfconv;\r\n\r\n\/\/ string conversion\r\nstd::wstring wide = L\"Hello, World! \uc548\ub155\ud558\uc138\uc694?\";  \/\/ wide string with utf-16 encoding\r\nstd::string narrow = utfconv.to_bytes(wide);     \/\/ conversion from utf-16 to utf-8\r\nwide = utfconv.from_bytes(narrow);               \/\/ back from utf-8 to utf-16\r\n\r\n\/\/ conversion during file I\/O\r\nstd::wofstream fout;                             \/\/ wide output stream\r\nfout.open(\"test.txt\", fout.out);\r\nfout.imbue(std::locale(fout.getloc(), new std::codecvt_utf8_utf16&lt;wchar_t&gt;));\r\nfout &lt;&lt; wide &lt;&lt; std::endl;                       \/\/ this stream is stored as utf-8\r\nfout &lt;&lt; utfconv.from_bytes(narrow) &lt;&lt; std::endl; \/\/ the same as the above line\r\nfout.close();\r\n\r\nstd::wifstream fin;\r\nfin.open(\"test.txt\", fin.in);\r\nfin.imbue(std::locale(fin.getloc(), new std::codecvt_utf8_utf16&lt;wchar_t&gt;));\r\nstd::wstring hello, world, anyoung, tline;\r\nfin &gt;&gt; hello &gt;&gt; world &gt;&gt; anyoung;      \/\/ utf-8 stream is converted to utf-16 string\r\nstd::getline(fin, tline);              \/\/ read out the end of the line\r\nstd::getline(fin, tline);              \/\/ read the next line\r\nfin.close();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;locale&gt; #include &lt;codecvt&gt; \/\/ Unicode representation in MS Windows uses the 2-byte wchar_t type. std::wstring_convert&lt;std::codecvt_utf8_utf16&lt;wchar_t&gt;, wchar_t&gt; utfconv; \/\/ string conversion std::wstring wide = L&#8221;Hello, World! \uc548\ub155\ud558\uc138\uc694?&#8221;; \/\/ wide string with utf-16 encoding std::string narrow = utfconv.to_bytes(wide); \/\/ conversion from utf-16 to utf-8 wide = utfconv.from_bytes(narrow); \/\/ back from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[89,28,35],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-computer","tag-unicode","tag-visual-studio","tag-windows"],"_links":{"self":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":0,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"wp:attachment":[{"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaewon.hwang.info\/jaewon\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}