LoadFromFile with Unicode Data

My input file (f) has Unicode (Swedish) which is not read correctly.

None of these approaches work, although they give different results:

LoadFromFile(f); 

or

  LoadFromFile(f,TEncoding.GetEncoding(GetOEMCP)); 

I am using Delphi XE

How can I load LoadFromFile with some Unicode data ... also how can I save SaveToFile? Thanks

+6
source share
2 answers

To load a text file in Unicode, you need to know its encoding. If the file has a byte order sign (BOM), you can simply call LoadFromFile(FileName) , and RTL will use the specification to determine the encoding.

If there is no specification in the file, you need to explicitly specify the encoding, for example.

 LoadFromFile(FileName, TEncoding.UTF8); LoadFromFile(FileName, TEncoding.Unicode);//UTF-16 LE LoadFromFile(FileName, TEncoding.BigEndianUnicode);//UTF-16 BE 

For some reason, unknown to me, there is no native support for UTF-32, but if you had such a file, it would be enough to simply add a TEncoding instance to handle this.

+10
source

I assume you mean UTF-8 when you say Unicode.

If you know the file is UTF-8, then

 LoadFromFile(f, TEncoding.UTF8). 

To save:

 SaveToFile(f, TEncoding.UTF8); 

(The GetOEMCP WinAPI function is for old 255-character character sets.)

+7
source

Source: https://habr.com/ru/post/915568/


All Articles