Re: buf to integer?

2019-02-08 Thread Simon Proctor
There's probably a nicer way but I don't generally play about with this
sort of thing.

:16([~] $x.reverse.map( *.base(16) ))

It does involve lots of String manipulation, as I say. There's probably a
better way.


On Fri, 8 Feb 2019 at 06:35, Todd Chester via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> 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
> 725C5DAE
>
>
> Is there a more "elegant" way to do this?
>
> Many thanks,
> -T
>


-- 
Simon Proctor
Cognoscite aliquid novum cotidie

http://www.khanate.co.uk/


Re: buf to integer?

2019-02-08 Thread The Sidhekin
On Fri, Feb 8, 2019 at 7:36 AM Todd Chester via perl6-users <
perl6-users@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
> 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


Re: buf to integer?

2019-02-08 Thread Brad Gilbert
If you have a new enough version of Rakudo:

my Buf $x=Buf.new(0xAE,0x5D,0x5C,0x72);

my int32 $i = $x.read-int32(0,LittleEndian);

say $i.base(16);
# 725C5DAE

On Fri, Feb 8, 2019 at 12:35 AM Todd Chester via perl6-users
 wrote:
>
> Hi All,
>
> 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
> 725C5DAE
>
>
> Is there a more "elegant" way to do this?
>
> Many thanks,
> -T


Re: buf to integer?

2019-02-08 Thread Simon Proctor
Ooo. Nice.

On Fri, 8 Feb 2019, 17:55 Brad Gilbert  If you have a new enough version of Rakudo:
>
> my Buf $x=Buf.new(0xAE,0x5D,0x5C,0x72);
>
> my int32 $i = $x.read-int32(0,LittleEndian);
>
> say $i.base(16);
> # 725C5DAE
>
> On Fri, Feb 8, 2019 at 12:35 AM Todd Chester via perl6-users
>  wrote:
> >
> > Hi All,
> >
> > 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
> > 725C5DAE
> >
> >
> > Is there a more "elegant" way to do this?
> >
> > Many thanks,
> > -T
>


Re: shift left syntax?

2019-02-08 Thread Brad Gilbert
The `=` infix operator is a meta operator.

That means it takes an infix operator as a sort of "argument".

There is no `+=` operator, it is just the `=` operator combined with
the `+` operator.

$a += 2;
$a [+]= 2; # more explicitly take the + operator as an argument to
the = operator

So if you want to know how to use a similar operator to `+=`, start
with the infix operator you want, and add `=`

$i = $i +< 0x01;

$i [+<]= 0x01;
$i +<= 0x01;

On Fri, Feb 8, 2019 at 12:20 AM Todd Chester via perl6-users
 wrote:
>
> Hi All,
>
> Is this the only way to shift left?
>
>$i = $i +< 0x01
>
> $ p6 'my int32 $i=0x5DAE; say $i.base(0x10); $i = $i +< 0x01; say
> $i.base(0x10);'
>
> 5DAE
> BB5C
>
>
> Does we have any of those fancy += ~= ways of doing it?
>
> Many thanks,
> -T


Re: shift left syntax?

2019-02-08 Thread ToddAndMargo via perl6-users

>>
>> Hi All,
>>
>> Is this the only way to shift left?
>>
>> $i = $i +< 0x01
>>
>> $ p6 'my int32 $i=0x5DAE; say $i.base(0x10); $i = $i +< 0x01; say
>> $i.base(0x10);'
>>
>> 5DAE
>> BB5C
>>
>>
>> Does we have any of those fancy += ~= ways of doing it?
>>
>> Many thanks,
>> -T

On 2/8/19 10:02 AM, Brad Gilbert wrote:

The `=` infix operator is a meta operator.

That means it takes an infix operator as a sort of "argument".

There is no `+=` operator, it is just the `=` operator combined with
the `+` operator.

 $a += 2;
 $a [+]= 2; # more explicitly take the + operator as an argument to
the = operator

So if you want to know how to use a similar operator to `+=`, start
with the infix operator you want, and add `=`

 $i = $i +< 0x01;

 $i [+<]= 0x01;
 $i +<= 0x01;

On Fri, Feb 8, 2019 at 12:20 AM Todd Chester via perl6-users
 wrote:


Thank you!


Re: buf to integer?

2019-02-08 Thread ToddAndMargo via perl6-users

On 2/7/19 10:35 PM, Todd Chester via perl6-users wrote:

Hi All,

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
725C5DAE


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

Many thanks,
-T


Hi All,

Thank you for all the wonderful tips!  Now I have to
write them all down for my own documentation.

This is a fun one I also came up with.  Since I was already
doing bitwise shift left, I thought I might as well do
it all with bitwise operations: first bitwise shift left,
then bitwise OR to combine them.  (Yes, I played with AND
and OR gates 40 years ago.  CMOS was my favorite.)

$ 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
725C5DAE


Interesting that I did not have to put () to force the order of
operation.  "shift" came before "OR".

-T


Re: buf to integer?

2019-02-08 Thread ToddAndMargo via perl6-users

On 2/8/19 2:34 AM, Simon Proctor wrote:
There's probably a nicer way but I don't generally play about with this 
sort of thing.


:16([~] $x.reverse.map( *.base(16) ))

It does involve lots of String manipulation, as I say. There's probably 
a better way.


Thank you!


Re: buf to integer?

2019-02-08 Thread ToddAndMargo via perl6-users

On 2/8/19 2:59 AM, The Sidhekin wrote:

   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



Thank you!


Re: buf to integer?

2019-02-08 Thread ToddAndMargo via perl6-users

On 2/8/19 9:54 AM, Brad Gilbert wrote:

If you have a new enough version of Rakudo:

 my Buf $x=Buf.new(0xAE,0x5D,0x5C,0x72);

 my int32 $i = $x.read-int32(0,LittleEndian);

 say $i.base(16);
 # 725C5DAE


Thank you!


Re: buf to integer?

2019-02-08 Thread Kevin Pye
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  wrote:

> On Fri, Feb 8, 2019 at 7:36 AM Todd Chester via perl6-users <
> perl6-users@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
>> 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
>
>


Re: buf to integer?

2019-02-08 Thread ToddAndMargo via perl6-users

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 > wrote:


On Fri, Feb 8, 2019 at 7:36 AM Todd Chester via perl6-users
mailto:perl6-users@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
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!


Re: buf to integer?

2019-02-08 Thread ToddAndMargo via perl6-users

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 so


I started using Perl at Perl 5.  I jumped to Perl 6 as soon as
I got a load of the sub routine declaration  clean up.  I write
in Top Down and live and die with subs.  Perl 5's sub declarations
are a pain in the ...

As far as my buf to integer problem, I will just write
a sub and use shift and OR.  Hmmm.   Top Down again.
Maybe I will make it into a module if I see myself using
it in other programs.  I worked a lot with AND and OR gates
many years ago and it brings back memories.


Re: buf to integer?

2019-02-08 Thread yary
the perl6-native "read-int32" or native-but-experimental "unpack" are the
natural answers- they map well from problem to answer! There's a good
example of perl6's pack in this thread.

read-int32 was mentioned without an example so...

my Buf $x = Buf.new(0x00, 0xFF, 0x88, 0xAE,0x5D,0x5C,0x72);

Buf:0x<00 FF 88 AE 5D 5C 72>

> $x.read-int32(3,LittleEndian).base(0x10)

===SORRY!=== Error while compiling:

Undeclared name:

LittleEndian used at line 1

 Oh darn, need Perl6.d for read-int32 and the Endian enums! Is 6.d packaged
for general use?

-y

>
>


Re: buf to integer?

2019-02-08 Thread Brad Gilbert
The next release of Rakudo with have read-int32.

To use it now you would need to build from the git repository, I think.

On Fri, Feb 8, 2019 at 7:56 PM yary  wrote:
>
> the perl6-native "read-int32" or native-but-experimental "unpack" are the 
> natural answers- they map well from problem to answer! There's a good example 
> of perl6's pack in this thread.
>
> read-int32 was mentioned without an example so...
>
> my Buf $x = Buf.new(0x00, 0xFF, 0x88, 0xAE,0x5D,0x5C,0x72);
>
> Buf:0x<00 FF 88 AE 5D 5C 72>
>
> > $x.read-int32(3,LittleEndian).base(0x10)
>
> ===SORRY!=== Error while compiling:
>
> Undeclared name:
>
> LittleEndian used at line 1
>
>
>  Oh darn, need Perl6.d for read-int32 and the Endian enums! Is 6.d packaged 
> for general use?
>
> -y
>>
>>