{"id":2178,"date":"2021-05-14T19:17:25","date_gmt":"2021-05-14T17:17:25","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=2178"},"modified":"2021-07-17T09:37:54","modified_gmt":"2021-07-17T07:37:54","slug":"unicode-vs-windows-console","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/2178","title":{"rendered":"Unicode vs. Windows Console"},"content":{"rendered":"\n<p>If I run this, what the output would be?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string&gt;\n#include &lt;iostream&gt;\n\n#include &lt;winrt\\base.h&gt;\n#include &lt;winrt\\Windows.Foundation.h&gt;\n#include &lt;winrt\\Windows.Globalization.DateTimeFormatting.h&gt;\n\n#pragma comment(lib, \"windowsapp.lib\")\n\nint main()\n{\n\tauto const Now = winrt::clock::now();\n\twinrt::Windows::Globalization::DateTimeFormatting::DateTimeFormatter DateTimeFormatter { L\"shortdate longtime\" };\n\tstd::wcout &lt;&lt; \"Now is \" &lt;&lt; static_cast&lt;std::wstring&gt;(DateTimeFormatter.Format(Now)) &lt;&lt; std::endl;\n\treturn 0;\n}\n<\/code><\/pre>\n\n\n\n<p>Here we go:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"384\" src=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-800x384.png\" alt=\"\" class=\"wp-image-2179\" srcset=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-800x384.png 800w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-320x154.png 320w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-768x369.png 768w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-600x288.png 600w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1040x499.png 1040w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image.png 1054w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>What appears to be wrong is <a href=\"http:\/\/www.unicode-symbol.com\/u\/200E.html\">Unicode Left-to-Right mark character<\/a>, which is killing the console and it stops accepting any further text!<\/p>\n\n\n\n<p>Now if you&#8217;re going to do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string>\n#include &lt;iostream>\n\n#include &lt;winrt\\base.h>\n#include &lt;winrt\\Windows.Foundation.h>\n#include &lt;winrt\\Windows.Globalization.DateTimeFormatting.h>\n\n#pragma comment(lib, \"windowsapp.lib\")\n\nstd::wstring Replace(std::wstring const&amp; Input, std::wstring const&amp; A, std::wstring const&amp; B)\n{\n\tstd::wstring Output;\n\tfor(size_t C = 0; ; )\n\t{\n\t\tauto const D = Input.find(A, C);\n\t\tif(D == Input.npos)\n\t\t{\n\t\t\tOutput.append(Input.substr(C));\n\t\t\tbreak;\n\t\t}\n\t\tOutput.append(Input.substr(C, D - C));\n\t\tOutput.append(B);\n\t\tC = D + A.length();\n\t}\n\treturn Output;\n}\n\nint main()\n{\n\tauto const Now = winrt::clock::now();\n\twinrt::Windows::Globalization::DateTimeFormatting::DateTimeFormatter DateTimeFormatter { L\"shortdate longtime\" };\n\tstd::wcout &lt;&lt; \"Now is \" &lt;&lt; Replace(static_cast&lt;std::wstring>(DateTimeFormatter.Format(Now)), L\"\\u200E\", L\"\") &lt;&lt; std::endl;\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>Then you get what you want, and not the trailing EOL is in its place (and it&#8217;s not in the first run):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"384\" src=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1-800x384.png\" alt=\"\" class=\"wp-image-2180\" srcset=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1-800x384.png 800w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1-320x154.png 320w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1-768x369.png 768w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1-600x288.png 600w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1-1040x499.png 1040w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2021\/05\/image-1.png 1054w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2021-07-17<\/h2>\n\n\n\n<p>A clear way to unblock Unicode character output is probably to specify locale for console output explicitly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::wcout.imbue(std::locale(\".UTF8\"));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If I run this, what the output would be? Here we go: What appears to be wrong is Unicode Left-to-Right mark character, which is killing the console and it stops accepting any further text! Now if you&#8217;re going to do this: Then you get what you want, and not the trailing EOL is in its&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/2178\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[38,588,57],"class_list":["post-2178","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-c-winrt","tag-unicode"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=2178"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=2178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=2178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=2178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}