Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Equivalent of unpack 'q' with 32-bit Perl

by Limbic~Region (Chancellor)
on Sep 06, 2016 at 20:13 UTC ( [id://1171266]=perlquestion: print w/replies, xml ) Need Help??

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
I no longer code but I am helping someone update some legacy code that they "borrowed" from me a years ago.
use Digest::MD5 'md5'; my $foo = 'hello, world!'; print unpack('q', md5($foo));
They only have stock 32-bit Perl 5.10.1 and a very restricted environment (don't ask). Is there a way to do the above in 32-bit perl? They can use any module that shipped with 5.10.1 as well as any pure perl module that they could easily copy/paste.

Cheers - L~R

Replies are listed 'Best First'.
Re: Equivalent of unpack 'q' with 32-bit Perl (a8)
by Anonymous Monk on Sep 06, 2016 at 20:23 UTC

    Get the bits only?

    use constant CAN_PACK_QUADS => !! eval { my $f = pack 'q'; 1 }; sub Int64 { unpack( ( CAN_PACK_QUADS ? 'q<' : 'a8' ), $_[-1] ) } sub UInt64 { unpack( ( CAN_PACK_QUADS ? 'Q<' : 'a8' ), $_[-1] ) }
      Anonymous Monk,

      Get the bits only?

      Presumably you mean take the first 8 bytes and then convert them to an integer by extracting the individual bits and then doing base conversion.

      If that's not what you mean, please enlighten me. If that is what you mean, I would need to play with it because I don't think the b/B templates would be helpful, I think it would need vec

      Cheers - L~R

        No, I mean dont do any conversion if its not possible :) treat it as a string, convert as quad when you can, otherwise its raw bytes ...

        There is always bigint which says

        print hex("0x1234567890123490"),"\n"; # Perl v5.10.0 or later

        So

        use constant CAN_PACK_QUADS => !! eval { my $f = pack 'q'; 1 }; use Math::BigInt; print quad('ffffffffffff'), "\n"; sub quad { CAN_PACK_QUADS ? unpack( 'q<', $_[0] ) : Math::BigInt->new( '0x'.$_[0] ) } __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1171266]
Approved by Marshall
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found