5

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

3
  • What are you trying to do in the first place? How are you displaying the WideString output? Do you really need UTF-8, WideString is already Unicode? I'd be great to have some more details here. Commented Feb 7, 2012 at 16:03
  • @JensMühlenhoff The utf-8 would come from an xml file, and I would like to pass it to another library function which needs widestring. I use the watches in the debugger to check the value Commented Feb 7, 2012 at 16:19
  • I know this isnt always an option, but for Unicode support it is well worth considering upgrading to latest version of Delphi, as the Unicode support finally just works. Commented Feb 12, 2012 at 15:27

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I think Delphi 7 can handle UTF-8 encoded files. The .pas file is Utf-8 encoded without BOM (checked with notepad++), and it's displayed correctly in the Delphi IDE. I paste your MessageBoxW part to my delphi 7 code, and it displayed the correct text, looks like the Code insight can't display widestrings...
I don't store utf-8 strings in this way, I just wanted to give a quick example.. finally it turned out that the Code insight misleaded me. The utf8decode function works perfectly. thanks.
OK, but I still think this is the answer to the question you asked.
+1 "Trying to treat your string literals like this is simply not practical." is the correct answer.
@David I will accept your answer, because using MessageBoxW led me to the right direction. But The text I gave in my example is already an Utf-8 encoded text interpreted as Ansi, so in your example it's doubly interpreted. Your advice about not to put these kind of things into the .pas is very true, I fully agree. What do you think, should I change the title of this question to something else, or leave it like this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.