xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Howdy!

In order to settle the question as the title mentioned, I've tried many ways I can image:
perl -e "printf(qq(%b),1024*1024*1024*1024)" #overflow! perl -e "print unpack(qq(B*), pack(qq(L), 1024*1024*1024*1024)) # dit +to perl -e "print unpack(qq(B*), pack(qq(L2), 1024*1024*1024*1024)) # di +tto
I believe magic pack could definitely resolve this issue perfectly. But while I've read perldoc -f pack over and over, it confused me more and more. I have no choice but post my issue and hope gurus would enlighten me.
Thanks in advance!



I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: How turn a huge decimal number to binary
by BrowserUk (Patriarch) on Sep 29, 2007 at 10:48 UTC

    Within the limitations of doubles to represent large numbers accurately, you can do this by breaking the number into hi and lo integers, converting them separately, and concatenating the results:

    $huge = 1024**4;; $lo = $huge % 2**32;; $hi = int( $huge / 2**32 );; printf "%b%032b\n", $hi, $lo;; 10000000000000000000000000000000000000000

    Otherwise take a look at the bigint pragma and Math::BigInt.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How turn a huge decimal number to binary
by almut (Canon) on Sep 29, 2007 at 11:44 UTC

    You could do something like

    #!/usr/bin/perl use bigint; sub split32 { my $v = shift; my $n = shift; my $mask = 0xffffffff; my @b32; for (1..$n) { unshift @b32, $v & $mask; $v >>= 32; } return @b32; } my $n = 2; # n x 32-bit my $v = 1024; for (1..6) { print unpack("B*", pack("N$n", split32($v, $n) )), "\n"; $v *= 1024; }

    Prints

    0000000000000000000000000000000000000000000000000000010000000000 0000000000000000000000000000000000000000000100000000000000000000 0000000000000000000000000000000001000000000000000000000000000000 0000000000000000000000010000000000000000000000000000000000000000 0000000000000100000000000000000000000000000000000000000000000000 0001000000000000000000000000000000000000000000000000000000000000

    I've set the width to 64 bit in the example (to avoid line wrapping...), but in principle, you can also use larger widths, e.g. $n = 8 for 256 bits. Also, $v can of course be any integer number, and must not necessarily be a power of 1024.

    Update: BTW, don't indiscriminately enable bigint for your entire program... it can slow down things considerably! Limit its scope to where you really need it — by putting extra blocks around those sections:

    { use bigint; # code that needs it... } # other code...
Re: How turn a huge decimal number to binary
by lidden (Curate) on Sep 29, 2007 at 19:57 UTC
    This should work:
    #!/usr/bin/perl -w use strict; use Math::BigInt; my $number = '38562959738576293875632496'; my $n = Math::BigInt->new($number); print $n->as_bin(), "\n";
    You may want to remove the '0b' prefix though.
Re: How turn a huge decimal number to binary
by graff (Chancellor) on Sep 29, 2007 at 18:08 UTC
    I wonder what you are really trying to do, once you get the answer to this question. Where is your "huge decimal number" really coming from?

    If you are just playing with powers 1024, it might be good enough to know that the binary representation for 1024 is a "1" followed by 10 zeros. That is:

    $binary_string = "1" . "0" x 10; print "$binary_string\n"
    For each power of 1024, just add ten more zeros:
    $power = 4; $binary_string = "1" . "0" x (10 * $power); # parens required print "$binary_string\n"
    But I assume you are really trying to do something more complicated than that. For example, if you are trying to read a string of decimal digits of arbitrary length, and convert that to binary, I'm not sure anyone has posted a good solution for that yet. (I don't have a good solution for that, but I'm sure one can be found...)
Re: How turn a huge decimal number to binary (flex)
by tye (Sage) on Sep 30, 2007 at 03:53 UTC

    Use tilly's Math::Fleximal. And if you are curious how it accomplishes it, then read the source code (it implements its own whatever-number-base "big int" library).

    - tye        

Re: How turn a huge decimal number to binary
by xiaoyafeng (Deacon) on Sep 30, 2007 at 01:15 UTC
    Thanks for gurus' reply!
    Of course, data in my programme isn't a simple 1024**4, I just want to say the number is too big to wrap in 32bit.
    bigint I'll study soon seems a straightforward way to solve this issue, but I prefer BrowserUk 's trick since I don't need to load a big module.

    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction