Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: decimal to binary

by Khen1950fx (Canon)
on Jan 09, 2012 at 07:34 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found