简体   繁体   English

如何让Delphi和Android使用相同的字符集?

[英]How to make Delphi and Android use the same charset?

I'm doing a project for anwsering questionnaires. 我正在做一个关于问卷调查的项目。 The user creates the project with the questions on Delphi, than exports the project in a .txt file to Android, where the file is read and the user can answer. 用户使用Delphi上的问题创建项目,而不是将.txt文件中的项目导出到Android,在那里读取文件并且用户可以回答。 My problem is in characters like á,à,É,Ú, that appear like a ? 我的问题是á,à,É,Ú等字符,看起来像? in Android, with the code 65533. So, I need to know how to configure Android and Delphi to work in the same charset. 在Android中,代码为65533.因此,我需要知道如何配置Android和Delphi以在同一个字符集中工作。

Android is Linux based and so presumably uses UTF-8. Android基于Linux,因此可能使用UTF-8。 On the other hand, Android is also very Java-like and so possibly prefers UTF-16. 另一方面,Android也非常类似于Java,因此可能更喜欢UTF-16。


If you need the file to be UTF-8, you can do it like this, assuming you have your text in a TStringList . 如果您需要将文件设置为UTF-8,则可以这样做,假设您的文本位于TStringList

StringList.SaveToFile(FileName, TEncoding.UTF8);

This will include a BOM in the file which I imagine Android won't like—Windows UTF-8 apps tend to use BOMs, but not Linux. 这将包括文件中的BOM,我认为Android不会喜欢 - Windows UTF-8应用程序倾向于使用BOM,而不是Linux。 If you want to output without a BOM do it like this: 如果您想在没有BOM的情况下输出,请执行以下操作:

type
  TUTF8EncodingNoBOM = class(TUTF8Encoding)  
  public
    function GetPreamble: TBytes; override;
  end;

function TUTF8EncodingNoBOM.GetPreamble: TBytes;
begin
  Result := nil;
end;
...
var
  UTF8EncodingNoBOM: TEncoding;//make this a global variable
...
UTF8EncodingNoBOM := TUTF8EncodingNoBOM.Create;//create in a unit initialization, remember to free it
...
StringList.SaveToFile(FileName, UTF8EncodingNoBOM);

If you discover you need UTF-16 then use TEncoding.Unicode for UTF-16LE or TEncoding.BigEndianUnicode for UTF-16BE. 如果您发现需要UTF-16,则使用TEncoding.Unicode表示UTF-16LE或TEncoding.BigEndianUnicode表示UTF-16BE。 If you need to strip the BOM then that's easy enough with the same technique as above. 如果您需要剥离BOM,那么使用与上述相同的技术就足够了。

Summary 摘要

  1. Work out what encoding you need, and its endianness. 找出你需要的编码及其字节序。
  2. Find an appropriate TEncoding . 找到合适的TEncoding
  3. Use TStrings.SaveToFile with that TEncoding instance. TStrings.SaveToFile与该TEncoding实例一起使用。

Use Unicode. 使用Unicode。 UTF-16 or UTF-8 should work fine. UTF-16或UTF-8应该可以正常工作。

See Davids answer for an explanation why this should work and how to do it in D2009 and newer. 请参阅戴维斯的答案,了解为什么这应该有效,以及如何在D2009及更新版本中执行此操作。

For Delphi 2007 and older you have to use another solution, UTF8Encode + Ansi TStringList can be used, you can also convert your strings to WideStrings and use WideStrings. 对于Delphi 2007及更早版本,您必须使用另一种解决方案,可以使用UTF8Encode + Ansi TStringList,您也可以将字符串转换为WideStrings并使用WideStrings。

To write UTF-8 using D2007 and older see this question: 要使用D2007及更早版本编写UTF-8,请看这个问题:

How can a text file be converted from ANSI to UTF-8 with Delphi 7? 如何使用Delphi 7将文本文件从ANSI转换为UTF-8?

To write UTF-16 using D2007 you can use the WideStrings unit which contains a TWideStringList . 要使用D2007编写UTF-16,您可以使用包含TWideStringListWideStrings单元。 Beware that this class doesn't write the BOM by default. 请注意,默认情况下,此类不会写入BOM。

There are also other WideStringList implementations for older Delphi versions out there. 对于较旧的Delphi版本,还有其他WideStringList实现。

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

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