[Lazarus] Unpack of PHP

silvioprog silvioprog at gmail.com
Mon Feb 4 18:29:54 CET 2013


2013/2/4 silvioprog <silvioprog at gmail.com>

> 2013/2/4 Vincent Snijders <vincent.snijders at gmail.com>
>
>> 2013/2/4 silvioprog <silvioprog at gmail.com>:
>> > 2013/2/4 David Knaack <davidknaack at gmail.com>
>> >>
>> >> I may not understand the scope of your question, but could you use the
>> >> swap function?
>> >>
>> >> http://www.freepascal.org/docs-html/rtl/system/swap.html
>> >>
>> >> Like so (edited in email, not compile checked):
>> >>
>> >> function ReadMWord(AStream: TStream): Word;
>> >>   var
>> >>     MW: Word
>> >>   begin
>> >>     AStream.Read(MW, SizeOf(Word));
>> >>     Result := Swap(MW);
>> >>   end;
>> >
>> >
>> > Perfect! :)
>> >
>> > Thank you very much!
>>
>> Instead of swap, you may use beton, so that it works on big endian cpus
>> too:
>> http://www.freepascal.org/docs-html/rtl/system/beton.html
>>
>> Vincent
>>
>
> Many thanks guy, worked like a charm too! :) I'll use BEtoN instead Swap.
>

Oops... And this is the full code:

program project1;

{$mode objfpc}{$H+}

uses
  Classes,
  SysUtils;

  function ReadInt(AStream: TStream): Word;
  begin
    Result := 0;
    AStream.Read(Result, SizeOf(Word));
    Result := BEtoN(Result);
  end;

var
  VFile: TFileStream;
begin
  VFile := TFileStream.Create('data.ascii', fmOpenRead or fmShareDenyWrite);
  try
    VFile.Seek(2, 0);
    WriteLn(ReadInt(VFile));
    VFile.Seek(6, 0);
    WriteLn(ReadInt(VFile));
  finally
    VFile.Free;
  end;
end.

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20130204/e8e45248/attachment-0003.html>


More information about the Lazarus mailing list