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:
but the MessageBoxW could display the text:
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);
Trying to treat your string literals like this is simply not practical. You probably need to start putting them into resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With