Category: Utilities
Author/Contact Info majin
email - kh-gokuu@mnn.rr.com
ICQ - 152569654
AIM - dahitokiri
Description: A newbie script (I am a newbie...atleast in the world of Perl} that takes (almost) any integer and converts it into binary and makes sure that the amount of digits the user recieves in binary are a multiple of 8. Any comments or suggestions would be much appreciated.

Small revsion complete...

Update #2: Upon further inspection I have found that only unsigned integers work, that is, only positive integers and 0. but that is the only limit so far I have not found any positive integer that does not work, if you do find one please contact me about it using the various contact methods listed above. I will try to improve upon this code to allow the use of negative integers...

Update #3: Added new functanility of counting the number of bits displayed....going to add more...
#!/usr/bin/perl -w

print "Input number: ";
$num = <>;
chomp $num;

$array[0] = $num % 2;
while ($num > 1) {
   $num = int $num / 2;
   push(@array, $num % 2);
}

@array = reverse @array;
my $n = 1;
while (scalar(@array) > (8 * $n)) {
   $n++;
}

my $num = (8 * $n) - scalar(@array);
unshift (@array, 0 x $num);

print "\nBinary: ";
foreach(@array) {
print;
}
print "\n\n";
$totalbits = scalar(@array) + 1;
print "$totalbits\n";
Replies are listed 'Best First'.
Re: int2bin
by MrNobo1024 (Hermit) on Aug 11, 2002 at 17:40 UTC
    You're doing far more work than you have to. printf has a %b format for printing numbers out in binary:
    #!/usr/bin/perl -w print "Input number: "; my $num = <>; my $len = (length(sprintf("%b", $num)) + 7) & ~7; printf "\nBinary: %0${len}b\n", $num;

    --MrNobo1024
    s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

Re: int2bin
by Zaxo (Archbishop) on Aug 11, 2002 at 17:40 UTC

    Nice code. Perl has some slick ways of doing this sort of thing. You may have set off a round of golf. My ungolfed way:

    { local $\ = $/; for (@ARGV) { $_ = unpack 'B*', pack 'N', $_; s/^(?:0{8})*([01]{8,})/$1/g; print; } }
    Setting $\ makes newline after print happen. The pack unpack trick is at the heart of the method. The substitution trims zeros.

    Update: majin, there is a limit of 32 bits. The substitution s/0{8}//g was meant to remove leading groups of eight zeros, but was incorrect, it will remove any group of eight zeros. Corrected in code. We match groups of eight zeros from the left end, then capture the rest, substituting the captured part.

    After Compline,
    Zaxo

      Is there a limit to you code... say after 8 digits will it stop? Or does it remove the zeroes in the front of the dgits? Update: well to remove the leading zeroes in my code the code has to be changed to a simpler version of its self:
      #!/usr/bin/perl print "Input number: "; $num = <>; chomp $num; $array[0] = $num % 2; while ($num > 1) { $num = int $num / 2; push(@array, $num % 2); } print "Binary: "; foreach(reverse @array) { print; } print "\n";
      this shall not add the leading zeroes to the left of the digits but now it shall not remain to be multiples of 8...