Perl - Converting Dec to Binary

Get help on web editors (Frontpage, Dreamweaver) and web languages (HTML, ASP, PHP).
Post Reply
wit_skapie
Moderator Emeritus
Posts: 6866
Joined: 12 Dec 2003, 02:00
Location: JHB
Contact:

Perl - Converting Dec to Binary

Post 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:
wit_skapie
Moderator Emeritus
Posts: 6866
Joined: 12 Dec 2003, 02:00
Location: JHB
Contact:

Post by wit_skapie »

So i found a solution, anyone interested can pm me :D
Post Reply