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

replace

my $decimal = (join '', map sprintf('%0*d', $size, $_), reverse @numbe +r) =~ s/0+(?=.)//r;

with

s/0+(?=.)// for my $decimal = join '', map sprintf('%0*d', $size, $_), + reverse @number;

It's just doing math in base $n which is 10**18 in this case (because that's the largest power of 10 that works on my machine). Change $size to 1 to see it work (more slowly) in base 10.

It's doing the same kind of thing that 'bigint' does...

Replies are listed 'Best First'.
Re^4: converting binary to decimal
by harangzsolt33 (Deacon) on Jun 27, 2025 at 03:13 UTC
    The output I'm getting looks like this. And I don't know why. I tried both suggestions, and it's the same with both:

    000000001-00000000000000001-00000000000000001-00000000000000001-000000 +0000000000 1-00000000000000001-00000000000000001-00000000000000001-00000000000000 +001-000000 00000000001-00000000000000001-00000000000000001-00000000000000001-0000 +0000000000 001-00000000000000001-00000000000000001-00000000000000001-000000000000 +00001-0000 0000000000001-00000000000000001-00000000000000001-00000000000000001-00 +0000000000 00001-00000000000000001-00000000000000001-00000000000000001-0000000000 +0000001-00 000000000000001-00000000000000001-00000000000000001-00000000000000001- +0000000000 0000001-00000000000000001-00000000000000001-00000000000000001-00000000 +000000001- 00000000000000001-00000000000000001-00000000000000001-0000000000000000 +1-00000000 000000001-00000000000000001-00000000000000001-00000000000000001-000000 +0000000000 1-00000000000000001-00000000000000001-00000000000000001-00000000000000 +001-000000 00000000001-00000000000000001-00000000000000001-00000000000000001-0000 +0000000000 001-00000000000000001-00000000000000001-00000000000000001-000000000000 +00001-0000 0000000000001-00000000000000001-00000000000000001-00000000000000001-00 +0000000000 00001-00000000000000001-00000000000000001-00000000000000001-0000000000 +0000001-00 000000000000001-00000000000000001-00000000000000001-00000000000000001- +0000000000 0000001-00000000000000001-00000000000000001-00000000000000001-00000000 +000000001- 00000000000000001-00000000000000001-00000000000000001-0000000000000000 +1-00000000 000000001-00000000000000001-00000000000000001-00000000000000001-000000 +0000000000 1-00000000000000001-00000000000000001-00000000000000001-00000000000000 +001-000000 00000000001-00000000000000001-00000000000000001-00000000000000001-0000 +0000000000 001-00000000000000001-00000000000000001-00000000000000001-000000000000 +00001-0000 0000000000001-00000000000000001-00000000000000001-00000000000000001

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

        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";