Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: decimal to binary

by Khen1950fx (Canon)
on Jan 09, 2012 at 07:34 UTC ( #946957=note: print w/replies, xml ) Need Help??


in reply to decimal to binary

I used dec2bin from the Perl Cookbook.
sub dec2bin { my $str = unpack('B32', pack('N', shift)); $str =~ s/^0+(?=\d)//; return $str; }
For example:
#!/usr/bin/perl -l use strict; use warnings; $|=1; my(@dec) = qw(1 2 3 4 5 6 7 8 9 10 16 32 64 100 256 512 1000 1024); foreach my $dec (@dec) { print dec2bin( $dec ); } sub dec2bin { my $str = unpack('B32', pack('N', shift)); $str =~ s/^0+(?=\d)//; return $str; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://946957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-05-22 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?