AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

$0 in stream and loading as text

Ein Thema von WojTec · begonnen am 24. Aug 2014 · letzter Beitrag vom 25. Aug 2014
Antwort Antwort
brechi

Registriert seit: 30. Jan 2004
823 Beiträge
 
#1

AW: $0 in stream and loading as text

  Alt 25. Aug 2014, 13:31
You have to write your own component to visualize the text.
Internal PAnsiChar/PWideChar is used and a #0 is the end of the text.

So if you use

Code:
  Memo1.Lines.Text := 'test1'#0'test2'#0;
only the "test1" will be shown.

If you dont want to save your data, then you can load the file into a TMemoryStream (or a string) and replace the #0 with another character.


Code:
var
  s: String;
  fm: TFileStream;
fm := TFileStream.Create;
fm.LoadFromFile('sadasd', fmOpenRead);
setLength(s, fm,size);
fm.Read(s[1], Length(s));
s := StringReplacte(s, #0, #1, [rfReplaceAll]);
memo1.lines.text := s;
etc.
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.932 Beiträge
 
Delphi 12 Athens
 
#2

AW: $0 in stream and loading as text

  Alt 25. Aug 2014, 13:45
If you dont want to save your data, then you can load the file into a TMemoryStream (or a string) and replace the #0 with another character.
This is only unicode text, so the problem is not to replace those bytes, but to read the file with correct encoding.
If one did it this way and there is only one real unicode character inside, one would get really strange results...

Even if there is no byte order mark this code works perfectly for me:
Delphi-Quellcode:
var
  test: TStringStream;
begin
  test := TStringStream.Create('', TEncoding.Unicode);
  try
    test.LoadFromFile('c:\temp\a.txt');
    ShowMessage(test.DataString);
  finally
    test.Free;
  end;
end;
So it would be better, if you would attach a working sample code with an example file...
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#3

Re: $0 in stream and loading as text

  Alt 25. Aug 2014, 17:06
@Sir Rufo, next time I'll be more precise
Data producer is unknown, I just know it is some resource compiler + compiler + linker (mean file is executable). Data is some data, it can be in any format, it can be a string too.

I wrote simple function to check if stream can be Unicode or ANSII:

Delphi-Quellcode:
function GetEncoding(AStream: TStream): TEncoding;
var
  V: Byte;
begin
  Result := TEncoding.ANSI;

  if AStream.Size > 1 then
  begin
    AStream.Position := 1;
    AStream.ReadBuffer(V, 1);
    if V = 0 then
      Result := TEncoding.Unicode
    ;
  end;
end;
Another one, I badly loaded encoded text (LoadFromStream - it was raw data, stupilo ), Append(DataString) is required.

As @brechi told, pointer terminate code is zero in TStrings, so this is main problem. I don't know how jump over it

Also I wrote poor (because very slow) procedure to replace bytes in stream, if someone need it:

Delphi-Quellcode:
procedure ReplaceBytes(AIn: TStream; const AFrom, ATo: Byte; AOut: TStream);
var
  I: Cardinal;
  V: Byte;
begin
  for I := 1 to AIn.Size do
  begin
    AIn.ReadBuffer(V, 1);

    if V = AFrom then
      AOut.WriteBuffer(ATo, 1)
    else
      AOut.WriteBuffer(V, 1)
    ;
  end;
end;
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz