简体   繁体   English

Delphi XE2中的UTF8编码

[英]UTF8Encode in Delphi XE2

I'm trying to convert string using我正在尝试使用转换字符串

Var
 encode:ansistring;
begin
  encode:=UTF8Encode('اختبار');
  showmessage(encode);
end;

It's working fine in Delphi 7它在 Delphi 7 中工作正常

but in Delphi XE2 it's send Text as question marks但在 Delphi XE2 中它发送文本作为问号

Any suggestions?有什么建议么?

In your Delphi 7 code you probably wrote something like this:在您的 Delphi 7 代码中,您可能写了如下内容:

var
  UTF8: string;
  InputString: WideString;//I guess that you used WideString
.....
UTF8 := UTF8Encode(InputString);

This was fine in Delphi 7 where string is an alias for AnsiString .这在 Delphi 7 中很好,其中stringAnsiString的别名。 In XE2 the generic string type is now an alias for UnicodeString which is UTF-16 encoded.在 XE2 中,通用string类型现在是 UTF-16 编码的UnicodeString的别名。 That means that when the code above is compiled by XE2, the UTF-8 encoded buffer returned by UTF8Encode is interpreted as UTF-16 encoded text.这意味着当上面的代码由 XE2 编译时,UTF8Encode 返回的UTF8Encode编码缓冲区被解释为 UTF-16 编码文本。 And that mismatch is what leads to your string full of question marks.这种不匹配导致您的字符串充满问号。

So, if you just wrote所以,如果你只是写

var
  UTF8: AnsiString;
  InputString: string;//aliased to UnicodeString
.....
UTF8 := UTF8Encode(InputString);

then you would have the same behaviour as for your Delphi 7 code.那么您将具有与 Delphi 7 代码相同的行为。

However, this is not the way to do it in Unicode Delphi. Instead you should use the UTF8String type.但是,在Unicode Delphi 中,这不是这样做的方法。而是应该使用UTF8String类型。 This is defined as AnsiString(65001) which means a string of 8 bit character units with code page 65001 , ie the UTF-8 codepage.这被定义为AnsiString(65001) ,这意味着代码页为65001的 8 位字符单元字符串,即 UTF-8 代码页。 When you do this you don't need to call UTF8Encode at all since the encoding attached to the string type means that the compiler can generated code to convert the string.执行此操作时,根本不需要调用UTF8Encode ,因为附加到字符串类型的编码意味着编译器可以生成代码来转换字符串。 Now you would simply write:现在你只需写:

var
  UTF8: UTF8String;
  InputString: string;//aliased to UnicodeString
.....
UTF8 := InputString;

The principal reference for the Unicode aspects of Delphi 2009 and later is Marco Cantù's white paper: Delphi and Unicode which I recommend that you read before proceeding. Delphi 2009 及之后的 Unicode 方面的主要参考是Marco Cantù 的白皮书:Delphi 和 Unicode我建议您在继续之前阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM