in reply to Re^6: Equivalent of unpack 'q' with 32-bit Perl (a8)
in thread Equivalent of unpack 'q' with 32-bit Perl

After thinking about it for a minute, I don't think that would work. Basically, you have an 128 bit integer in binary form and you are then converting the first 64 bits of it into decimal form. I'm pretty sure a hex digest isn't going to help here because you can't simply just cut it in half.

That seems to be what unpack('q', md5($foo)) is doing - cutting the digest in half.

use Digest::MD5 'md5'; my $foo = 'hello, world!'; my @v = unpack('q', md5($foo)); printf "%x %x\n", @v;
$ perl unpackq.pl e3ba1f79d1badb3a 0

If you really need to convert the whole digest to an integer, try Math::BigInt->from_hex(md5_hex($foo))

Replies are listed 'Best First'.
Re^8: Equivalent of unpack 'q' with 32-bit Perl (a8)
by Limbic~Region (Chancellor) on Sep 07, 2016 at 20:54 UTC
    RonW,

    If you really need to convert the whole digest to an integer

    I just need to duplicate the result of unpack('q', md5($thing)) on 32bit Perl. I wrote something that works at home (see this thread) but it doesn't seem to work at work and I can't understand why.

    Cheers - L~R

      This seems to do what you are asking:

      #!perl use strict; use warnings; use Digest::MD5 qw(md5 md5_hex); use Math::BigInt; my $foo = 'hello, world!'; my @v = unpack('q', md5($foo)); printf "%x %x\n", @v; my $h = substr(md5_hex($foo),0,16); # get first 8 bytes (pairs of hex +digits) my @w = reverse $h =~ /(..)/g; # split out the bytes and reverse +the order my $w = join('', @w); my $q = Math::BigInt->from_hex($w); print $q->as_hex();

      I was surprised that it was necessary to reverse the bytes to get the same result as unpack('q', md5($foo)) but that's what it took.