Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Signed Long Hash

by breezykatt (Acolyte)
on Jun 15, 2017 at 15:36 UTC ( #1192867=perlquestion: print w/replies, xml ) Need Help??

breezykatt has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to convert a string value into a signed long hash value. By doing so, I first convert the string "abcd" to fnv64a hash value which is "18165163011005162717", using the use Digest::FNV::PurePerl module. Thereafter, I am trying to convert this to a signed long by using the Math::BigInt module and the numify method. However, this doesn't appear to be working. I am looking for the long signed value of "-281581062704388899". Is this possible? Thanks.

Replies are listed 'Best First'.
Re: Signed Long Hash
by choroba (Archbishop) on Jun 15, 2017 at 15:45 UTC
    Math::BigInt is for arbitrary precision, it doesn't understand boundaries like "signed long".

    If your Perl can handle quads in pack, you should be able to just use them, no bigint needed:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $hash = '18165163011005162717'; my $signed = unpack 'q', pack 'Q', $hash; # Using 'q' woudl work, too +. say $signed eq "-281581062704388899";
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      One follow up question - how does one convert a numeric value (eg -281581062704388899) to base64 in perl similar to java's call:


      to get the java base64 encoded string: '/Befg+4HJN0'

        See MIME::Base64 and perhaps pack.

        Update: Although I do not receive the same result - are you sure about the input and output?

        use strict; use warnings; use MIME::Base64 'encode_base64'; use Test::More tests => 1; is (encode_base64((pack 'q', -281581062704388899), ''), '/Befg+4HJN0') +;

        gives '3SQH7oOfF/w=' instead.

      Update: Sorry, posted in the wrong place, this was supposed to be an answer to the OP (see below)
Re: Signed Long Hash
by Laurent_R (Canon) on Jun 15, 2017 at 16:24 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1192867]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2020-09-25 07:18 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (136 votes). Check out past polls.