Page 1 of 1

Perl - Converting Dec to Binary

Posted: 23 Jun 2008, 10:34
by wit_skapie
Hi,

So the problem is that i need to convert a large decimal number to binary.

The script below works fine for most numbers:

sub dec2bin {
my $str = unpack("B32", pack("N", shift));
$str =~ s/^0+(?=\d)//;
return $str;
}

But if the decimal gets really big i only get "11111111111111111111........111111" returned.....

Anyone have a solution for this?

I'm working on a manual algorithm and will post it if i get it to work....

Cheers,
w_s

:biggrin:

Posted: 23 Jun 2008, 11:19
by wit_skapie
So i found a solution, anyone interested can pm me :D