What does this mean?
#!/usr/bin/perl
print "Start Base: ";
$base = <>;
chomp $base;
print "End Base: ";
$ebase = <>;
chomp $ebase;
print "Number: ";
$bin = <>;
chomp $bin;
$bin =~ s/^0+//;
@bin = split(//, $bin);
@bin = &convert(@bin);
$dec = shift @bin;
foreach $bit(@bin){
$dec *= $base;
$dec += $bit;
}
while(($dec / $ebase) >= 1){
$result .= $dec % $ebase;
$result .= "|";
$dec /= $ebase;
use POSIX;
$dec = floor($dec);
}
$result .= $dec;
print "Base $ebase: ", &unconvert($result);
sub convert {
my @ret_arr;
foreach $bit(@_){
if($bit !~ m/d/){
$bit= uc $bit;
push @ret_arr, (ord $bit) - 55;
}
else{push @ret_arr, $bit}
}
return @ret_arr;
}
sub unconvert {
my @ret_arr;
my @temp;
@temp = split /|/, $_[0];
foreach $bit(@temp){
if($bit > 9){unshift @ret_arr, chr ($bit +55)}
else{unshift @ret_arr, $bit}
}
return @ret_arr;
}
print "Start Base: ";
$base = <>;
chomp $base;
print "End Base: ";
$ebase = <>;
chomp $ebase;
print "Number: ";
$bin = <>;
chomp $bin;
$bin =~ s/^0+//;
@bin = split(//, $bin);
@bin = &convert(@bin);
$dec = shift @bin;
foreach $bit(@bin){
$dec *= $base;
$dec += $bit;
}
while(($dec / $ebase) >= 1){
$result .= $dec % $ebase;
$result .= "|";
$dec /= $ebase;
use POSIX;
$dec = floor($dec);
}
$result .= $dec;
print "Base $ebase: ", &unconvert($result);
sub convert {
my @ret_arr;
foreach $bit(@_){
if($bit !~ m/d/){
$bit= uc $bit;
push @ret_arr, (ord $bit) - 55;
}
else{push @ret_arr, $bit}
}
return @ret_arr;
}
sub unconvert {
my @ret_arr;
my @temp;
@temp = split /|/, $_[0];
foreach $bit(@temp){
if($bit > 9){unshift @ret_arr, chr ($bit +55)}
else{unshift @ret_arr, $bit}
}
return @ret_arr;
}
It would appear to convert numbers from one base to another. It should prompt you for a Start Base which would be - duhh - the base you're starting with, and end base, which is what you want to convert to, and a number to convert. So if you put in 10 for start base, 2 for end base, and 16 for the number, your result would be 10000, which is equivalent to 16 in binary, or base 2...
Actually, it's appears to be a pretty handy little script!
Actually, it's appears to be a pretty handy little script!
Thread
Thread Starter
Forum
Replies
Last Post




