{"id":4853,"date":"2005-09-11T08:00:00","date_gmt":"2005-09-11T08:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/2005\/09\/11\/how-wide-is-a-string-when-displayed-in-the-console-window\/"},"modified":"2005-09-11T08:00:00","modified_gmt":"2005-09-11T08:00:00","slug":"how-wide-is-a-string-when-displayed-in-the-console-window","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/how-wide-is-a-string-when-displayed-in-the-console-window\/","title":{"rendered":"How wide is a string when displayed in the console window?"},"content":{"rendered":"<p>Not too long ago, I had to fix the command line output for tf.exe, the Team Foundation Version Control command line app, so that output would be formatted properly for console windows (cmd.exe)&nbsp;using double-byte code pages.<\/p>\n<p>The code originally computed the output display width as the length of the string.&nbsp; However, that&#8217;s not correct when the code is running on&nbsp;a Japanese Windows system, for example.&nbsp; Whereas English letters all take up one position in the console window, double-byte characters are twice as wide and take two positions.&nbsp; The original code produced really bad looking output.<\/p>\n<p>To make sure that I got the right solution, I asked <a href=\"http:\/\/blogs.msdn.com\/team_foundation\/archive\/2005\/05\/09\/415838.aspx\">Aldo<\/a>,&nbsp;an internalization PM.&nbsp; The solution was to use the number of bytes that would result from converting the string to a byte array using the console window&#8217;s code page.&nbsp;&nbsp;Each English letter results in&nbsp;a byte, and&nbsp;each Japanese character results in two bytes.<\/p>\n<p>For single-byte code pages, the double-byte characters only take up one position, because they get munged due to the console not being able to display them.&nbsp; The .NET <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/86hf4sb8(en-US,VS.80).aspx\">System.Text.Encoding<\/a> class handles properly converting strings to and from byte arrays.&nbsp; Of course, I didn&#8217;t need an actual byte array.&nbsp; Fortunately, the Encoding class has a method called <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/w3739zdy\">GetByteCount()<\/a> that gives the information I need.<\/p>\n<p>The end result is the following simple method that calculates the display width of a string in&nbsp;the console&#8217;s encoding.<\/p>\n<p><font face=\"Courier New\">&nbsp;&nbsp;&nbsp; static int CalculateConsoleWidth(String text)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Encoding encoding = Console.Out.Encoding;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (encoding.IsSingleByte)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return text.Length;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return encoding.GetByteCount(text);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not too long ago, I had to fix the command line output for tf.exe, the Team Foundation Version Control command line app, so that output would be formatted properly for console windows (cmd.exe)&nbsp;using double-byte code pages. The code originally computed the output display width as the length of the string.&nbsp; However, that&#8217;s not correct when [&hellip;]<\/p>\n","protected":false},"author":94,"featured_media":10268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-4853","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-c"],"acf":[],"blog_post_summary":"<p>Not too long ago, I had to fix the command line output for tf.exe, the Team Foundation Version Control command line app, so that output would be formatted properly for console windows (cmd.exe)&nbsp;using double-byte code pages. The code originally computed the output display width as the length of the string.&nbsp; However, that&#8217;s not correct when [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/4853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/users\/94"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/comments?post=4853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/4853\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media\/10268"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media?parent=4853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=4853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=4853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}