September 11th, 2005

How wide is a string when displayed in the console window?

Buck Hodges
Director of Engineering

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) using double-byte code pages.

The code originally computed the output display width as the length of the string.  However, that’s not correct when the code is running on a Japanese Windows system, for example.  Whereas English letters all take up one position in the console window, double-byte characters are twice as wide and take two positions.  The original code produced really bad looking output.

To make sure that I got the right solution, I asked Aldo, an internalization PM.  The solution was to use the number of bytes that would result from converting the string to a byte array using the console window’s code page.  Each English letter results in a byte, and each Japanese character results in two bytes.

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.  The .NET System.Text.Encoding class handles properly converting strings to and from byte arrays.  Of course, I didn’t need an actual byte array.  Fortunately, the Encoding class has a method called GetByteCount() that gives the information I need.

The end result is the following simple method that calculates the display width of a string in the console’s encoding.

    static int CalculateConsoleWidth(String text)
    {
        Encoding encoding = Console.Out.Encoding;

        if (encoding.IsSingleByte)
        {
            return text.Length;
        }
        else
        {
            return encoding.GetByteCount(text);
        }
    }

Topics
C#

Author

Buck Hodges
Director of Engineering

Director of Engineering, Azure DevOps

0 comments

Feedback