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<ae 5d 5c 72>

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

Many thanks,

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<AE 5D 5C 72>

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


Reply via email to