G
Graham Drabble
Hi all,
I have been using a binary to decimal converter for number <2**16 for
a while now using
$bin = sprintf("%016b",$dec)
which works fine.
I've now had to try and do the same conversion for numbers up to 2**
48 and have discovered that this does not work for numbers >=2**32
(everything >=2**32 returns the same as (2**32)-1 ).
I'm currently getting round this with
use strict;
use warnings;
use Math::BigInt;
my $num = new Math::BigInt 2**47 + 2**31 + 2**30;
if ($num >= 2**32){
my $upper = int($num / (2**32));
my $lower = $num % 2**32;
$upper = sprintf ("%016b",$upper);
$lower = sprintf ("%032b",$lower);
print "$upper$lower\n";
}
else
{
printf ("%048b\n",$num);
}
Which works but seems very inelegant. Does anyone have a better
suggestion?
Also hex() returns a warning when given a hex string >2**32 but
returns the correct result. Is this something that can be relied on
or do I need to find a work around for that as well?
I have been using a binary to decimal converter for number <2**16 for
a while now using
$bin = sprintf("%016b",$dec)
which works fine.
I've now had to try and do the same conversion for numbers up to 2**
48 and have discovered that this does not work for numbers >=2**32
(everything >=2**32 returns the same as (2**32)-1 ).
I'm currently getting round this with
use strict;
use warnings;
use Math::BigInt;
my $num = new Math::BigInt 2**47 + 2**31 + 2**30;
if ($num >= 2**32){
my $upper = int($num / (2**32));
my $lower = $num % 2**32;
$upper = sprintf ("%016b",$upper);
$lower = sprintf ("%032b",$lower);
print "$upper$lower\n";
}
else
{
printf ("%048b\n",$num);
}
Which works but seems very inelegant. Does anyone have a better
suggestion?
Also hex() returns a warning when given a hex string >2**32 but
returns the correct result. Is this something that can be relied on
or do I need to find a work around for that as well?