Search code examples
perlradix

Issues with Math::BaseCalc in Perl


I'm trying to use the Math::BaseCalc module to convert a base64 number into hexadecimal. Here is my code so far:

#!/usr/bin/env perl
use Math::BaseCalc;

my $calc64 = new Math::BaseCalc(digits =>['+','/','A'..'Z','a'..'z',0..9]);
my $calcHex = new Math::BaseCalc(digits =>[0..9,'a'..'f']);
$bvals="NU5+MSHRUIy07OQNb20WAc";
$btemp=$calc64->from_base($bvals);
$bhex=$calcHex->to_base($btemp);
print "$bhex\n";

The output of this is always 0. I have tried many example numbers in $bvals, I have tried reodering the digits for base64, moving the + and / symbols from the beginning to the end, and in the middle etc, all to no avail. It worked with exactly one number for bvals, but I refuse to believe it was working properly because the number it gave me was 32. There's no way a number of that size could be represented by 32.

Is this number simply too large? If so, what can I do?

EDIT: In conclusion, the numbers were too large (regardless of version) and I was using version 1.014. My solution looks like this:

my $calc64 = new Math::BaseCalc(digits =>['+','/','A'..'Z','a'..'z',0..9]);
my $calcHex = new Math::BaseCalc(digits =>[0..9,'a'..'f']);
$bvals="FztKXccUj73D8ZAPlnAfmE";
$btemp=Math::BigInt->new($calc64->from_base($bvals));
$bhex=$calcHex->to_base($btemp);
print "$bhex\n";

Solution

  • In conclusion, the numbers were too large (regardless of version) and I was using version 1.014. My solution looks like this:

    my $calc64 = new Math::BaseCalc(digits =>['+','/','A'..'Z','a'..'z',0..9]);
    my $calcHex = new Math::BaseCalc(digits =>[0..9,'a'..'f']);
    $bvals="FztKXccUj73D8ZAPlnAfmE";
    $btemp=Math::BigInt->new($calc64->from_base($bvals));
    $bhex=$calcHex->to_base($btemp);
    print "$bhex\n";