Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-08

Unicode output on console works for me on Linux, not on Windows

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

(Reposted from the forum)

I tried to output Unicode text to console on English Windows Vista (using Lucida Console font, which shows Greek and Cyrillic characters). Doesn't seem to work.
(On Debian x64 it works perfectly - maybe because the default encoding I've got is UTF8: LANG environment variable is en_US.utf8?) If I redirect the output to file, I can see the output properly for the output containing "plain" if I open the file as UTF8.

Does anybody know how to do this?

Thanks,
Reinier

{$mode objfpc}{$H+}
program UnicodeTest;

uses

  {$IFDEF UNIX}
    {$IFDEF UseCThreads}
    cthreads,
    {$ENDIF}
  {Widestring manager needed for widestring support}
  cwstring,
  {$ENDIF}
  Classes,
  StrUtils
  ;
var
UTF8TestString: string;
OurWideString: widestring;
begin
UTF8TestString:= ' rosé, водка and ούζο';
writeln ('plain: ' + UTF8TestString);
writeln (UTF8ToANSI('utf8toansi: ' + UTF8TestString));
writeln (UTF8Decode('utf8decode: ' + UTF8TestString));
//Just for luck, shouldn't work:
writeln (ANSIToUTF8('ansitoutf8: ' + UTF8TestString));
writeln (UTF8Encode('utf8encode: ' + UTF8TestString));
writeln ('');
OurWideString:= 'rosé, водка and ούζο';
writeln ('plain: ' + OurWideString);
writeln (UTF8ToANSI('utf8toansi: ' + OurWideString));
writeln (UTF8Decode('utf8decode: ' + OurWideString));
//Just for luck, shouldn't work:
writeln (UTF8Encode('utf8encode: ' + OurWideString));
writeln (ANSIToUTF8('ansitoutf8: ' + OurWideString));
end.

Reply by email

Marcos Douglas
to Reinier Olislagers
Reinier Olislagers wrote 50 lines
(Reposted from the forum) I tried to output Unicode text to console on English Windows Vista (using Lucida Console font, which shows Greek and Cyrillic characters). Doesn't seem to work. (On Debian x64 it works perfectly - maybe because the default encoding I've got is UTF8: LANG environment variable is en_US.utf8?) If I redirect the output to file, I can see the output properly for the output containing "plain" if I open the file as UTF8. Does anybody know how to do this? Thanks, Reinier {$mode objfpc}{$H+} program UnicodeTest; uses  {$IFDEF UNIX}    {$IFDEF UseCThreads}    cthreads,    {$ENDIF}  {Widestring manager needed for widestring support}  cwstring,  {$ENDIF}  Classes,  StrUtils  ; var UTF8TestString: string; OurWideString: widestring; begin UTF8TestString:= ' rosé, водка and ούζο'; writeln ('plain: ' + UTF8TestString); writeln (UTF8ToANSI('utf8toansi: ' + UTF8TestString)); writeln (UTF8Decode('utf8decode: ' + UTF8TestString)); //Just for luck, shouldn't work: writeln (ANSIToUTF8('ansitoutf8: ' + UTF8TestString)); writeln (UTF8Encode('utf8encode: ' + UTF8TestString)); writeln (''); OurWideString:= 'rosé, водка and ούζο'; writeln ('plain: ' + OurWideString); writeln (UTF8ToANSI('utf8toansi: ' + OurWideString)); writeln (UTF8Decode('utf8decode: ' + OurWideString)); //Just for luck, shouldn't work: writeln (UTF8Encode('utf8encode: ' + OurWideString)); writeln (ANSIToUTF8('ansitoutf8: ' + OurWideString)); end.

In console programs, try UTF8ToConsole() function (unit FileUtil). A string "created" outside of your code, use SysToUTF8() to convert in a UTF8 and after UTF8ToConsole() to print on the console.

Marcos Douglas

Reply by email

Jonas Maebe
to Reinier Olislagers: (Reposted from the forum) I tried to output Unicode text to console on English Windows…
Reinier Olislagers wrote
I tried to output Unicode text to console on English Windows Vista (using Lucida Console font, which shows Greek and Cyrillic characters). Doesn't seem to work.

I think that you have to configure the console to use UTF-8 output (but I don't know how this is done on Windows). A bit like how on *nix UTF-8 input/output also only works if the console is configured for it (using e.g. LANG=en_US.UTF-8).

(On Debian x64 it works perfectly - maybe because the default encoding I've got is UTF8: LANG environment variable is en_US.utf8?)

Indeed.

Jonas

Reply by email

Reinier Olislagers
to Ondrej
Ondrej wrote 7 lines
> I think that you have to configure the console to use UTF-8 output (but I > don't know how this is done on Windows). I think that should be: SetConsoleOutputCP(CP_UTF8);

Thanks all, I got it to work using SetConsoleOutput. UTF8ToConsole did not work for me.

For posterity/the archives:
program uniconsole;
{$mode objfpc}{$H+}
{$APPTYPE CONSOLE}
uses
  {$IFDEF UNIX}
    {$IFDEF UseCThreads}
    cthreads,
    {$ENDIF}
  {Widestring manager needed for widestring support}
  cwstring,
  {$ENDIF}
  {$IFDEF WINDOWS}
  Windows, {for setconsoleoutputcp}
  {$ENDIF}
  Classes
  ;
var
UTF8TestString: string;
begin
{$IFDEF WINDOWS}
SetConsoleOutputCP(CP_UTF8);
{$ENDIF}
UTF8TestString:= 'rosé, водка and ούζο';
writeln ('plain: ' + UTF8TestString);
{Apparently we don't need UTF8ToConsole for this
UTF8ToConsole did not do anything for me in fact.}
end.

Reply by email