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

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

Replies are listed 'Best First'.
Re^5: converting binary to decimal
by tybalt89 (Monsignor) on Jun 27, 2025 at 04:08 UTC

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

        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+(?=.)//