A console window that gets on with Unicode – fancy that

Like quite possibly a good proportion of people syndicated on DelphiFeeds.com, I’ve recently acquired a Mac. Being cheap, it’s only a refurbished iMac, but it looks good (and was spotless when taken out of the box).

In terms of first impressions, I’ve found OS X a bit ‘meh’ overall so far. Glancing through a couple Lion reviews (being last year’s model, my iMac only has Snow Leopard) suggests one annoyance (windows only resizable from one corner) has been fixed. Maybe OS X Tiddles will finally add proper, standardised maximise/restore functionality too? Nonetheless, one thing the Mac definitely beats Windows on is the standard console window (‘terminal’) properly supporting Unicode. Here’s a simple test program:

program ConsoleVsUnicode;


{$R *.res}


  SetTextCodePage(Output, CP_UTF8);
  WriteLn('NB: make sure a font like Consolas has been set as the console ' + 
    'font, else' + sLineBreak + 'this won''t work at all. Even then, the ' + 
    'third line of Chinese text probably still won''t work.');
  WriteLn('Not targetting Windows, so no special handling. Let''s see the result!');
  WriteLn('Γειά σου Κόσμε');
  WriteLn('Xin chào thế giới');
  Write('Press ENTER to exit...');

The SetConsoleOutputCP and SetTextCodePage calls are needed since the Windows console doesn’t support Unicode at all by default (as an aside, SetTextCodePage is new to XE2 – before you could only set the default ‘Ansi’ code page globally). Nonetheless, you are still likely to end up with a crap result in Windows-land whatever you do:

On the Mac though, things work fine, and minus the workarounds needed on Windows too:

Pretty neat I think.

6 thoughts on “A console window that gets on with Unicode – fancy that

    • Does that actually make my demo output the Chinese characters correctly on your computer? For me it doesn’t, the main issues being how the stock fixed width fonts don’t include any Chinese characters, and font substitution doesn’t work like it would in a GUI application.

    • Thanks. Can you tell me where exactly this setting is? As I said to Lars, Chinese characters (even ones requiring surrogate pairs) show fine in a GUI application, for which characters from the Arial Unicode MS font are automatically substituted as necessary. Also, can you confirm that means my demo actually does what it should do for you?

  1. Thanks for your great Delphi blog. While reading your blog, I was surprised when I got this ” WriteLn(‘Xin chào thế giới’);”
    It’s Vietnamese, not Chinese. How can you get this hello world from 🙂 ?
    BTW, great blog, I’m following you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s