Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-02

Unpack of PHP

14 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

In FPC, I'm trying to load a ASCII content and onvert it to integer, but, without success. In PHP I do it easily.

I send two files in attached:

1. php - demo in PHP working fine (the demo uses unpack function:
http://www.php.net/manual/en/function.unpack.php);
2. fpc - demo in FPC partially implemented;

So, how to do it in FPC?

Thank you!

ps. I tried the Unpack function of FPC, but I don't know how to use it.

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130204/a5a5231d/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: ascii_int.zip Type: application/zip Size: 2612 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130204/a5a5231d/attachment.zip>

Reply by email

ik
to silvioprog
silvioprog wrote 12 lines
Hello, In FPC, I'm trying to load a ASCII content and onvert it to integer, but, without success. In PHP I do it easily. I send two files in attached: 1. php - demo in PHP working fine (the demo uses unpack function: http://www.php.net/manual/en/function.unpack.php); 2. fpc - demo in FPC partially implemented; So, how to do it in FPC?

Take a look at my fp-msgpack[1] (I need to rewrite it, because the way that array and maps works), but it does exactly what pack and upack does.

Show quoted text 8 lines
Thank you! ps. I tried the Unpack function of FPC, but I don't know how to use it. -- Silvio Cl?cio My public projects - github.com/silvioprog

[1] https://github.com/ik5/fp-msgpack

Show quoted text 7 lines
-- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

silvioprog
to ik: Take a look at my fp-msgpack[1] (I need to rewrite it, because the way that array and…

2013/2/4 ik

Show quoted text 29 lines
On Mon, Feb 4, 2013 at 1:34 PM, silvioprog wrote: > Hello, > > In FPC, I'm trying to load a ASCII content and onvert it to integer, but, > without success. In PHP I do it easily. > > I send two files in attached: > > 1. php - demo in PHP working fine (the demo uses unpack function: > http://www.php.net/manual/en/function.unpack.php); > 2. fpc - demo in FPC partially implemented; > > So, how to do it in FPC? Take a look at my fp-msgpack[1] (I need to rewrite it, because the way that array and maps works), but it does exactly what pack and upack does. > > Thank you! > > ps. I tried the Unpack function of FPC, but I don't know how to use it. > > -- > Silvio Cl?cio > My public projects - github.com/silvioprog [1] https://github.com/ik5/fp-msgpack

I have not found the line that does the conversion (from ASCII to integer). The your code is big. I will need to study it to understand it. In PHP was so easy (only two lines), that I would have swore that something native and easy to use with Free Pascal.

Thank you!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130204/77de88da/attachment.html>

Reply by email

silvioprog
to leledumbo: Unpack() is for unpacking previously Pack()-ed data View this message in context:…

2013/2/4 leledumbo

> ps. I tried the Unpack function of FPC, but I don't know how to use it. Unpack() is for unpacking previously Pack()-ed data

So, I'm trying to improve the PowerPDF to support PNG images, but I dont know how to convert ASCII to Integer.

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

Reply by email

silvioprog
to silvioprog: 2013/2/4 leledumbo So, I'm trying to improve the PowerPDF to support PNG images, but I…

2013/2/4 silvioprog

Show quoted text 10 lines
2013/2/4 leledumbo > > ps. I tried the Unpack function of FPC, but I don't know how to use it. > > Unpack() is for unpacking previously Pack()-ed data > So, I'm trying to improve the PowerPDF to support PNG images, but I dont know how to convert ASCII to Integer.

Solved:

  function ReadMWord(AStream: TStream): Word;
  type
    TMotorolaWord = record
      case Byte of
        0: (Value: Word);
        1: (Byte1, Byte2: Byte);
      end;
  var
    MW: TMotorolaWord;
  begin
    AStream.Read(MW.Byte2, SizeOf(Byte));
    AStream.Read(MW.Byte1, SizeOf(Byte));
    Result := MW.Value;
  end;

:)

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130204/7819f139/attachment.html>

Reply by email

David Knaack
to silvioprog

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;
silvioprog wrote 43 lines
2013/2/4 silvioprog > 2013/2/4 leledumbo > >> > ps. I tried the Unpack function of FPC, but I don't know how to use it. >> >> Unpack() is for unpacking previously Pack()-ed data >> > > So, I'm trying to improve the PowerPDF to support PNG images, but I dont > know how to convert ASCII to Integer. > Solved: function ReadMWord(AStream: TStream): Word; type TMotorolaWord = record case Byte of 0: (Value: Word); 1: (Byte1, Byte2: Byte); end; var MW: TMotorolaWord; begin AStream.Read(MW.Byte2, SizeOf(Byte)); AStream.Read(MW.Byte1, SizeOf(Byte)); Result := MW.Value; end; :) -- Silvio Cl?cio My public projects - github.com/silvioprog -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130204/62c1961e/attachment-0001.html>

Reply by email

silvioprog
to David Knaack

2013/2/4 David Knaack

Show quoted text 15 lines
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!

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

Reply by email

Vincent Snijders
to silvioprog

2013/2/4 silvioprog :

Show quoted text 21 lines
2013/2/4 David Knaack > > 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

Reply by email

silvioprog
to Vincent Snijders

2013/2/4 Vincent Snijders

Show quoted text 29 lines
2013/2/4 silvioprog : > 2013/2/4 David Knaack >> >> 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.

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

Reply by email

silvioprog
to silvioprog

2013/2/4 silvioprog

Show quoted text 34 lines
2013/2/4 Vincent Snijders > 2013/2/4 silvioprog : > > 2013/2/4 David Knaack > >> > >> 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.freepascal.org/pipermail/lazarus/attachments/20130204/e8e45248/attachment.html>

Reply by email

Marco van de Voort
to silvioprog
silvioprog wrote 30 lines
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.

That's so boring! In FPC 2.7.1 this should work:

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

Reply by email

silvioprog
to Marco van de Voort

2013/2/6 Marco van de Voort
[...]

Show quoted text 21 lines
That's so boring! In FPC 2.7.1 this should work: uses Classes, SysUtils, StreamEx; var VFile: TFileStream; begin VFile := TFileStream.Create('data.ascii', fmOpenRead or fmShareDenyWrite); try VFile.Seek(2, 0); WriteLn(VFile.ReadWordBE); VFile.Seek(6, 0); WriteLn(VFile.ReadWordBE); finally VFile.Free; end; end.

ReadWordBE is very nice. Thank you very much guy! :)

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130211/517099a7/attachment.html>

Reply by email