On 2/8/19 1:37 PM, Kevin Pye wrote:
Unpack is very useful if you have multiple items you want to unpack, and if you're familiar with the Perl 5 unpack then there's the P5pack module (which isn't a full implementation of Perl 5's unpack, but is useful for simpler things). If you want to unpack something from the middle of a Buf or Blob then you'll need to explicitly skip over the beginning of the buffer using "x", whereas read-int32 has an explicit position argument.

On Fri, 8 Feb 2019 at 22:00, The Sidhekin <sidhe...@gmail.com <mailto:sidhe...@gmail.com>> wrote:

    On Fri, Feb 8, 2019 at 7:36 AM Todd Chester via perl6-users
    <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:

        I am dealing with a Buf what includes 32 bit integers, but
        they are entered somewhat backwards as view with hexedit:

        AE 5D 5C 72 represents the number 725C5DAE

        This is what I have come up with to convert this type of
        number in a buffer to and integer

        $ p6 'my Buf $x=Buf.new(0xAE,0x5D,0x5C,0x72); my int32 $i=$x[3]
        +< 0x18
        +  $x[2] +< 0x10  +  $x[1] +< 0x08  +  $x[0];  say $x; say
        $i.base(0x10);'

        Buf:0x<ae 5d 5c 72>
        725C5DAE


        Is there a more "elegant" way to do this?


       The "elegant" way I'd do it, is using unpack():
    https://docs.perl6.org/routine/unpack

       It's experimental, so a declaration is needed, but Buf does Blob,
    so otherwise, it's straight to the point:

    $ perl6 -e 'use experimental :pack; my Buf
    $x=Buf.new(0xAE,0x5D,0x5C,0x72); say $x.unpack("L").base(0x10);'
    725C5DAE
    $


    Eirik



Interesting.  Thank you!

Reply via email to