Results 1 to 6 of 6

Thread: Record to Memorystream

  1. #1
    Win32.Trojan.Heur.Herby
    Join Date
    Dec 2003
    Location
    Nuenen of all places
    Posts
    289

    Record to Memorystream

    Hoi

    Ik probeer de inhoud van een Record weg te schrijven naar een MemoryStream, de stream heb ik elders weer nodig.
    Wat ik ook doe, ik krijg de size van de record weggeschreven, niet de size met de inhoud van de Array (fixed size array werkt prima).
    Hoe kan ik dit het beste aanpakken?


    Alvast bedankt,
    Herby


    Code:
    program ArrayRecordTest;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils,
      System.Classes;
    
    //---------------------------------------------------------------------------\\
    type
      TByteArray  = array of byte;
    //---------------------------------------------------------------------------\\
    function StrToByteArray(aValue: String): TByteArray;
    var
      I : integer;
    begin
      SetLength(result, Length(aValue));
      for I := 0 to Length(aValue) - 1 do
        result[I] := ord(aValue[I + 1]);
    end;
    //---------------------------------------------------------------------------\\
    function ByteArrayToStr(aValue: TByteArray): string;
    var
      I   : integer;
      Ch  : char;
    begin
      result := '';
      for I := Length(aValue)-1 Downto 0 do
        begin
          Ch := Chr(aValue[I]);
          result := Ch + result;
        end;
    end;
    //---------------------------------------------------------------------------\\
    
    
    
    
    type
      TMyRecord     = record
        a           : byte;                 // 1 byte
        b           : integer;              // 4 bytes
        cLen        : integer;              // 4 bytes
        c           : TByteArray;           // 4 bytes --> zonder inhoud
        d           : array [1..5] of byte; // 5 bytes
      end;                                  //18 bytes
    
    var
      MyRecord      : TMyRecord;
      MS            : TMemoryStream;
      I             : byte;
    
    const
      MyString      = 'BlahBlahBlahBlah';   // 16 bytes
    
    begin
    
      // Fill MyRecord
      FillChar ( MyRecord, SizeOf(MyRecord), #0);
      MyRecord.a    := 1;
      MyRecord.b    := 2;
      MyRecord.cLen := Length(MyString);
      MyRecord.c    := StrToByteArray(MyString);
      for I := 1 to 5 do
        MyRecord.d[I] := I;
    
      WriteLn ('c = ' + ByteArrayToStr(MyRecord.c));
    
      // Fill MemoryStream with MyRecord
      MS := TMemoryStream.Create;
      try
        // dit Scrijft de groote naar de MS, niet de inhoud + de bytearray.
        MS.Write(MyRecord, SizeOf(MyRecord));
        MS.SaveToFile('output.dat');
      finally
        MS.Free;
      end;
    
    
      readln;
    end.

  2. #2
    Die schrijft vrolijk de pointer naar de array in de stream, niet helmaal wat je wilt.
    Hetzelfde gebeurt als je een record op deze manier naar een bestand wegschrijft.
    Je zult ofwel de individuele verlden moetn wegschrijven, of de array na het record, voorafgegaan door Length(c).
    Ongeveer zoals je AnsiString wegschrijft naar een stream.

    Bart

  3. #3
    Win32.Trojan.Heur.Herby
    Join Date
    Dec 2003
    Location
    Nuenen of all places
    Posts
    289
    Ja zoiets ben ik nu aan het doen, houdt in dat ik dit voor iedere bytearray moet doen.
    Toch bedankt

  4. #4
    Maak een Helper for TStream die een TByteArray wegschrijft?

    Bart

  5. #5
    waarom een stream gebruiken en geen (bijvoorbeeld) een dynamic array van records?

  6. #6
    Win32.Trojan.Heur.Herby
    Join Date
    Dec 2003
    Location
    Nuenen of all places
    Posts
    289
    Omdat ik de data in een stream moet hebben. deze stream wordt dan elders gebruikt als header in een file

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •