Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: converting binary to decimal

by tybalt89 (Monsignor)
on Jun 27, 2025 at 04:08 UTC ( [id://11165484]=note: print w/replies, xml ) Need Help??


in reply to Re^4: converting binary to decimal
in thread converting binary to decimal

<fightingsarcasm>It would be helpful to see the code that produces this output.</fightingsarcasm>

Replies are listed 'Best First'.
Re^6: converting binary to decimal
by harangzsolt33 (Deacon) on Jul 02, 2025 at 15:51 UTC
    Sure. Here is the code:

    #!/usr/bin/perl use strict; use warnings; my $size = 18; my $n = 10 ** $size; my @number = 0; my $binary = 1 . 0 x 8192; for ( split //, $binary ) { my $carry = $_; for ( @number ) { $_ += $_ + $carry; $carry = int $_ / $n; $_ %= $n; } $carry and push @number, $carry; } s/0+(?=.)// for my $decimal = join '', map sprintf('%0*d', $size, $_), + reverse @number; print "$decimal\n";

      Are you running a 32-bit perl? It seems your sprintf has a problem with 18 digit numbers. Try changing $size to 8 or 9. (I don't have a 32-bit perl to test on.)

      Also replace s/0+(?=.)// with s/^0+(?=.)//

        THANK YOU! IT WORKS!!!! Yes, when I am on Windows, it is 32-bit.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11165484]
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: (3)
As of 2025-11-17 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 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.