Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf8decode in delphi 7

I need to use delphi 7 to convert strings from utf8 to widestring. Could anybody tell me why the following code doesn't work in delphi 7? The parameter of the Utf8Decode function is just a sample.

var ws: WideString;
begin
   ws := Utf8Decode('[أمبير] خطأ تيار- تيار Ů…ŘŞŮاصل Ů…Ř·Ů„Ů‚');
end;

In delphi 7 it gives me lot's of question marks,however in bds2006 it works well.

Do I need to switch some compiler directive on, or how can I convert an utf8String to Widestring in delphi 7?

SOLUTION

There's nothing wrong with the Utf8Decode function, The Delphi Code Insight Tooltip expression evaluation output misled me, which can't display Widestrings. see the image below:

Tooltip expression evaluation

but the MessageBoxW could display the text:

enter image description here

like image 281
balazs Avatar asked Feb 07 '12 13:02

balazs


1 Answers

I believe that the problem is that Delphi 7 can only use ANSI for source files. Later versions of Delphi will use UTF-8 for source files and in fact you can specify what encoding you wish to use for your source files.

If you interpret the UTF-8 encoded string as ANSI (for example using Notepad++) then you can embed a UTF-8 encoded literal in an ANSI source code file. For example this code produces a message box with your text in using Delphi 6.

ws := UTF8Decode('[ŘÅمبير] خط؊تيار- تيار Ů…ŘŞŮاصل Ů…Ř·Ů„Ů‚');
MessageBoxW(0, PWideChar(ws), 
  PWideChar(WideString(FloatToStr(CompilerVersion))), 0);

enter image description here

Trying to treat your string literals like this is simply not practical. You probably need to start putting them into resources.

like image 199
David Heffernan Avatar answered Nov 01 '22 13:11

David Heffernan