Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Binary to Decimal (zdog's way)

by zdog (Priest)
on Aug 31, 2000 at 10:07 UTC ( [id://30478]=CUFP: print w/replies, xml ) Need Help??

I was playing around with the shift operator and I kept getting tired of converting binary to decimal. So I came up with this little program that I guess could be implemented different ways, but this is the way I like to use it.

After I wrote the program, I came to PerlMonks and saw that there was already a way to do this, obviously better, but I kind of feel proud of this script since I made it all on my own. So ...

#!/usr/bin/perl # Implementation: perl *.pl BINARY_NUM my @inputr = reverse split(//, shift); my $output = 0; for (0 .. @inputr-1) { $output += ($inputr[$_] * (2**$_)); } print "$output\n";

Replies are listed 'Best First'.
RE: Binary to Decimal (zdog's way)
by merlyn (Sage) on Aug 31, 2000 at 14:56 UTC
    Since you reference it, let me add that most of us use the far more compact and easy to retype and demonstratably faster code like:
    $_ = shift; print unpack "N", pack "B*", ("0" x (32 - length)) . $_;

    -- Randal L. Schwartz, Perl hacker

Re: Binary to Decimal (zdog's way)
by I0 (Priest) on Dec 30, 2000 at 18:44 UTC
    in v5.6.0 oct"0b$_"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2025-12-16 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (97 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.