Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi How to use Unicode text in streams? (https://www.delphipraxis.net/155187-how-use-unicode-text-streams.html)

WojTec 12. Okt 2010 11:25

Delphi-Version: 2010

How to use Unicode text in streams?
 
How to use Unicode strings in streams? I need to save strings in binary stream - for this I'm using TStringStream, but string are stored as ASCII. :(

Delphi-Quellcode:
var
  S: TStringStream;
begin
  S := TStringStream.Create;
  S.Encoding.UTF8;
  S.WriteString('象');
  S.SaveToFile('C:\0.txt');
  S.Free;

  S := TStringStream.Create;
  S.Encoding.UTF8;
  S.LoadFromFile('C:\0.txt');
  Caption := S.DataString;
  S.Free;
end;

Bummi 12. Okt 2010 11:39

AW: How to use Unicode text in streams?
 
Delphi-Quellcode:
  Encoding := TEncoding.Unicode;
  S := TStringStream.Create('象',Encoding);
Delphi-Quellcode:
  ss := '象';
  S := TStringStream.Create;
  S.Encoding.Unicode;
  S.WriteString(ss);

himitsu 12. Okt 2010 12:30

AW: How to use Unicode text in streams?
 
Zitat:

Delphi-Quellcode:
S.Encoding.Unicode;

Falsch, da man den Wert ja setzen und nicht ein Property davon auslesen will,
also theoretisch wäre es so richtig
Delphi-Quellcode:
S.Encoding := TEncoding.Unicode;
.


Aber leider ist .Encoding readonly, also geht es nur im Contructor.
Delphi-Quellcode:
S := TStringStream.Create('象', TEncoding.UTF8);

// oder

S := TStringStream.Create('', TEncoding.UTF8);
S.WriteString('象');

WojTec 12. Okt 2010 12:41

Re: How to use Unicode text in streams?
 
Encoding property is read only, so I did'n knew how to use it, I don't saw encoding parameter in constructor. Now working very well, thanks guys :D


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:05 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz