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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Base10 to Base2.
by thor (Priest) on Oct 21, 2002 at 18:52 UTC
    The question is: do you know how to do it "by hand"? Here's my process (the number that you want to convert is n):
    1. Find the largest k such that 2k ≤ n. Your binary number k+1 digits, starting with a 1
    2. Subtract 2k from your n.
    3. Subtract 1 from k.
    4. If 2k is larger than n, put a 0 down in your binary number. If k is smaller or equal to n, place a 1 in your binary number, and subtract 2k from n.
    5. redo the previous step until k is 0.
    Now, here's a simple example for n=10:
    1. 20 = 1 ≤ 10,
      21 = 2 ≤ 10,
      22 = 4 ≤ 10,
      23 = 8 ≤ 10,
      24 = 16 > 10.
      Thus, k=3, and our binary digit will have 4 bits, starting with a 1.
    2. n=10-23=2,
      k=3-1=2
    3. 22 = 4 > 2, so we put a 0.
      k=2-1=1
    4. 21 = 2 ≤ 2, so we put a 1.
      k=1-1=0,
      n=2-21=0
    5. 20 = 1 > 0, so we put a 0.
    6. So, our net result is 1010
    From this, you should be able to code your algorithm. A for loop comes to mind...;)

    thor

      More general: how to convert number n to base b.

      1. bk ≤ n; find k; k = floor( logbn )
      2. t * bk ≤ n; find t; t = floor( n/bk )
      3. put sign nr. t from your base at end of resulting string, there must be exactly b signs in the base array.
      4. set n = n - t * bk
      5. go to step 1 or finish if n = 0

      in other words:

      sub decimalToX { my $number = shift or return $_[0]; my $base = @_; my $logbase = log $base; my $string = ''; my $power = int(log($number)/$logbase); while($number){ my $f = $base ** $power; my $times = int($number/$f); $string .= $_[$times]; $number -= $times * $f; } continue { --$power } $string . $_[0] x ($power+1) } print decimaltoX( 5 , qw/0 1/ ); # 101
      --
      http://fruiture.de
        To paraphrase my college analysis professor: I was going to leave that as an exercise to the reader. ;)

        thor

Re: Base10 to Base2.
by sauoq (Abbot) on Oct 21, 2002 at 18:50 UTC
    $binary = sprintf('%b', $decimal);
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Base10 to Base2.
by husoft (Monk) on Oct 21, 2002 at 18:05 UTC
    Hello monk, first I recommend you to search before posting.
    You can see the same question clicking here.
    My way to do this is the following:
    #!/usr/bin/perl print "Enter number (DECIMAL): ";chomp($dec=<STDIN>); while($dec){ $dec = int($dec); @base = reverse(split(/\^*/,$dec)); $intB = int($base[0]/2); $noIntB = $base[0]/2; if($intB eq $noIntB){$temp="0"} else{$temp="1"}push(@binary,$temp); $dec /= 2; if($dec < 1){last} } print "Binary : " . reverse(@binary),"\n";
      Another implementation of (essentially) this same algorithm:
      print "Enter a decimal number: "; chomp( $decimal = <STDIN> ); $binary = ''; while( $decimal ){ $binary = $decimal%2 . $binary; $decimal = int( $decimal/2 ); } print STDOUT $binary;
      I think tadman's answer in the previous thread was more elegant (but apparently it only works in 5.6 or higher):
      my ($binary) = sprintf ("%08b", $decimal);
      --

      Love justice; desire mercy.
Re: Base10 to Base2. (or "Why I don't like this SoPW")
by charnos (Friar) on Oct 21, 2002 at 18:57 UTC
    This *does* smell like homework, firstly. My apologies if it is not, but the hurried, incomplete description and the egregious disuse of builtin functions to perform (then) trivial calculations is suspicious. This question really isn't necessarily a Perl question either, but a general engineering question, since you're not making use of Perl's rich library of functions. There are functions not including those two which do aid tasks like this though, in the case of my example/answer, map.
    my ($acc,$digit_count,$dec_string); $digit_count = 1; $dec_string = "1101110"; map {$acc += $_*$digit_count;$digit_count *=2;} reverse split '',$dec_ +string; print $acc;
    This simply splits a decimal string into a list of characters, then reverses them to preserve order. The map statement is performed on all values in the list in order: the list moves leftward, incrementing by powers of 2, and adding the increment counter to an accumulator variable if the current digit is 1. This doesn't perform any checking however, you asked how to convert from decimal to binary, and this does it. Any non-zero-or-one value will *work*, but will not produce the expected value.
Re: Base10 to Base2.
by rir (Vicar) on Oct 21, 2002 at 19:07 UTC
    This avoids assuming the size of an integer.
    sub dec_2_bin { my @nib=qw (000 001 010 011 100 101 110 111); map $nib[$_], split( //, sprintf "%lo", $_[0]); };
Re:(Golf) Base10 to Base2.
by BrowserUk (Patriarch) on Oct 22, 2002 at 00:34 UTC

    #! perl -sw use strict; # 1 2 3 4 #23456789012345678901234567890123456789012345 sub d2b{my$d=pop;($d>1?d2b($d>>1):'').($d&1)}

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Base10 to Base2.
by thelenm (Vicar) on Oct 21, 2002 at 18:36 UTC
    What do you mean by "decimal" and "binary"? Do you mean string representations of decimal and binary? Do a search for "decimal binary" and you will find some useful discussions.

    On a side note, why don't you want to use pack or unpack? The seemingly gratuitous restriction smells like homework...

    -- Mike

    --
    just,my${.02}