开发者

Does TReader/TWriter support unicode?

开发者 https://www.devze.com 2023-01-06 13:08 出处:网络
I\'m currently spending some time converting my network unit to support unicode strings and things are going good so far (at least I think so). Unfortunately I\'m working on Delphi 7 so I can\'t test

I'm currently spending some time converting my network unit to support unicode strings and things are going good so far (at least I think so). Unfortunately I'm working on Delphi 7 so I can't test the unicode support in action and I don't have documentation of the newer Delphi versions.

I have used a few TReader and TWriter objects in my code to stream strings with the ReadString and WriteString methods. On D7 they use the String type which equals AnsiString there. If I have understood right, in D2009/D2010 the String type equals UnicodeString. So does anyone know if TReader and TWriter automatically streams strings in UnicodeString format when using the ReadString and WriteString methods in D2009/D2010 or do I need to chan开发者_如何学Pythonge something?


Yes. TReader and TWriter support Unicde. In fact there is only one part of the vcl/rtl that does not support Unicode: the old style Read(Ln) and Write(Ln) file access methods which are also used to read and write directly from and to a console.

Yes, string is UnicodeString in D2009+. And PChar is PWideChar in D2009+. So if you stick to the standard types, your code will port fine.

You will only get into trouble when you base code on the assumption that one character equals one byte.

Update:

  • White paper Delphi and Unicode, by Marco Cantu: http://edn.embarcadero.com/article/38980

  • Migrating Legacy Apps to Unicode-enabled Delphi 2010, by Cary Jensen: http://edn.embarcadero.com/article/40472

  • New White Paper: Delphi Unicode Migration for Mere Mortals, by Cary Jensen: http://edn.embarcadero.com/article/40307


I just looked at my Delphi 2010 install, and TWriter has the following methods:

procedure WriteStr(const Value: AnsiString);
procedure WriteUTF8Str(const Value: string);
procedure WriteString(const Value: UnicodeString);
procedure WriteWideString(const Value: UnicodeString);

TReader has equivalent methods. So WriteStr (and ReadStr) will work with AnsiStrings for backwards compatibility, WriteString and WriteWideString will write a unicode string out as unicode, and WriteUTF8Str accepts a unicode string and writes it out as UTF8. (Which saves a lot of space if you're using the ANSI charset.)

0

精彩评论

暂无评论...
验证码 换一张
取 消