delphi 7中的utf8decode [英] utf8decode in delphi 7
问题描述
我需要使用delphi 7将字符串从utf8转换为widestring。谁能告诉我为什么以下代码在delphi 7中不起作用? Utf8Decode函数的参数只是一个示例。
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;
在delphi 7中,它给了我很多问号,但是在bds2006中它很好用。
In delphi 7 it gives me lot's of question marks,however in bds2006 it works well.
我是否需要打开某些编译器指令,或者如何在delphi 7中将utf8String转换为Widestring?
Do I need to switch some compiler directive on, or how can I convert an utf8String to Widestring in delphi 7?
解决方案
Utf8Decode函数没有问题,Delphi Code Insight工具提示表达式评估输出使我迷惑了,它无法显示宽字符串。参见下图:
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:
,但MessageBoxW可以显示文本:
but the MessageBoxW could display the text:
推荐答案
我认为问题在于Delphi 7只能将ANSI用于源文件。更高版本的Delphi将对源文件使用UTF-8,实际上,您可以指定要对源文件使用的编码。
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.
如果您解释UTF- 8编码的字符串作为ANSI(例如使用Notepad ++),则可以将UTF-8编码的文字嵌入ANSI源代码文件中。例如,这段代码使用Delphi 6生成一个带有您的文本的消息框。
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.
这篇关于delphi 7中的utf8decode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!