windows - Is Writeln capable of supporting Unicode? -
consider program:
{$apptype console} begin writeln('АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ'); end.
the output on console uses consolas font is:
????????z??????????????????????????????????????
the windows console quite capable of supporting unicode evidenced program:
{$apptype console} uses winapi.windows; const text = 'АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ'; var numwritten: dword; begin writeconsole(getstdhandle(std_output_handle), pchar(text), length(text), numwritten, nil); end.
for output is:
АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ
can writeln
persuaded respect unicode, or inherently crippled?
just set console output codepage through setconsoleoutputcp()
routine codepage cp_utf8
.
program project1; {$apptype console} uses system.sysutils,windows; const text = 'АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ'; var numwritten: dword; begin readln; // make sure consolas font selected try writeconsole(getstdhandle(std_output_handle), pchar(text), length(text), numwritten, nil); setconsoleoutputcp(cp_utf8); writeln; writeln('АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ'); except on e: exception writeln(e.classname, ': ', e.message); end; readln; end.
outputs:
АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ
writeln()
translates unicode utf16 strings selected output codepage (cp_utf8) internally.
update:
the above works in delphi-xe2 , above. in delphi-xe need explicit conversion utf-8 make work properly.
writeln(utf8string('АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ'));
addendum:
if output console done in codepage before calling setconsoleoutputcp(cp_utf8)
, os not correctly output text in utf-8
. can fixed closing/reopening stdout handler.
another option declare new text output handler utf-8
.
var toututf8: textfile; ... setconsoleoutputcp(cp_utf8); assignfile(toututf8,'',cp_utf8); // works in xe2 , above rewrite(toututf8); writeln(toututf8,'АБВГДЕЖЅzЗИІКЛМНОПҀРСТȢѸФХѾЦЧШЩЪЫЬѢѤЮѦѪѨѬѠѺѮѰѲѴ');
Comments
Post a Comment