while(1) {
$answer = 0;
print "Number to convert?\n";
chomp($input = <STDIN>);
print "Base to convert from?\n";
chomp($base = <STDIN>);
print "Base to convert to?\n";
chomp($base2 = <STDIN>);
while($base > 62 or $base < 2) {
if($base > 62) {
print "ERROR: Base cannot exceed 62!\n";
}
if($base < 2) {
print "ERROR: Base cannot be lower than 2!\n";
}
chomp($base = <STDIN>);
}
while($base2 > 62 or $base2 < 2) {
if($base2 > 62) {
print "ERROR: Base cannot exceed 62!\n";
}
if($base2 < 2) {
print "ERROR: Base cannot be lower than 2!\n";
}
chomp($base2 = <STDIN>);
}
print "\nBASE $base: $input\n";
for($i = length($input) - 1; $i >= 0; $i--) {
$sub_input = substr($input,$i,1);
if($sub_input =~ /A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z/){
$sub_input = ord("$sub_input") - 55;
}
if($sub_input =~ /a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z/){
$sub_input = ord("$sub_input") - 61;
}
$answer += $sub_input * ($base ** (($i - length($input) + 1) * -1));
}
print "BASE 10: $answer\n";
$output = "";
$i = $base2;
while($i < $answer) {
$i = $i * $base2;
}
while($i >= 1) {
if($answer < $i) {
$output .= "0";
}
else {
$fix = $answer%$i;
$fix = $answer - $fix;
$num = $fix/$i;
$answer -= ($i * $num);
if($num <= 9) {
$output .= $num;
}
if($num > 9 and $num <= 35) {
$num2 = $num + 55;
$output .= chr("$num2");
}
if($num > 35) {
$num2 = $num + 61;
$output .= chr("$num2");
}
}
$i = $i/$base2;
}
$out2 = "";
if(substr($output,0,1) eq "0") {
for($j = 1; $j<= length($output); $j++) {
$out2 .= substr($output,$j,1);
}
print "BASE $base2: $out2\n\n";
}
else {
print "BASE $base2: $output\n\n";
}
}
Here's the base converter I made. It's written in perl and can convert from any base between 2 and 62.
use Tk;
BEGIN {
if ($^O eq 'MSWin32') {
require Win32::Console;
Win32::Console::Free( );
}
}
my $main_window = new MainWindow;
my $frame = $main_window -> Frame();
my $lab = $frame -> Label(-text=>"(Original) Base: 2");
my $ent = $frame -> Text(-width=>50, -height=>3);
my $srl_y = $frame -> Scrollbar(-orient=>'v',-command=>[yview => $ent]);
$srl_y -> grid(-row=>2,-column=>8,-sticky=>"ns");
$ent -> configure(-yscrollcommand=>['set', $srl_y],);
my $lab2 = $frame -> Label(-text=>"Base: 10");
my $ent2 = $frame -> Text(-width=>50, -height=>3);
my $srl_y2 = $frame -> Scrollbar(-orient=>'v',-command=>[yview => $ent2]);
$srl_y2 -> grid(-row=>3,-column=>8,-sticky=>"ns");
$ent2 -> configure(-yscrollcommand=>['set', $srl_y2],);
my $lab3 = $frame -> Label(-text=>"(New) Base: 2");
my $ent3 = $frame -> Text(-width=>50, -height=>3);
my $srl_y3 = $frame -> Scrollbar(-orient=>'v',-command=>[yview => $ent3]);
$srl_y3 -> grid(-row=>4,-column=>8,-sticky=>"ns");
$ent3 -> configure(-yscrollcommand=>['set', $srl_y3],);
my $but = $main_window -> Button(-text=>"Convert!", -command =>\&move_slider);
$but -> grid(-row=>5,-column=>1,-columnspan=>2);
my $scl = $frame -> Scale(-label=>"Original Base :",
-orient=>'h', -digit=>1,
-from=>2, -to=>62,
-variable=>\$base, -tickinterval=>0, -length => 200, -command => \&move_slider);
$scl -> grid(-row=>1,-column=>1);
my $scl2 = $frame -> Scale(-label=>"New Base :",
-orient=>'h', -digit=>1,
-from=>2, -to=>62,
-variable=>\$base2, -tickinterval=>0, -length => 200, -command => \&move_slider);
$scl2 -> grid(-row=>1,-column=>2);
$lab -> grid(-row=>2,-column=>1);
$ent -> grid(-row=>2,-column=>2);
$frame -> grid(-row=>1,-column=>1,-columnspan=>2);
$lab2 -> grid(-row=>3,-column=>1);
$ent2 -> grid(-row=>3,-column=>2);
$lab3 -> grid(-row=>4,-column=>1);
$ent3 -> grid(-row=>4,-column=>2);
MainLoop;
sub move_slider {
$lab -> configure(-text=>"(Original) Base: $base");
$lab3 -> configure(-text=>"(New) Base: $base2");
$answer = 0;
my $input = $ent -> get('1.0','end');
for($i = length($input) - 1; $i >= 0; $i--) {
$sub_input = substr($input,$i,1);
if($sub_input =~ /A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z/){
$sub_input = ord("$sub_input") - 55;
}
if($sub_input =~ /a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z/){
$sub_input = ord("$sub_input") - 61;
}
$answer += $sub_input * ($base ** (($i - length($input) + 2) * -1));
}
$ent2 -> delete('0.0', 'end');
$ent2 -> insert('end',"$answer");
$output = "";
$i = $base2;
while($i < $answer) {
$i = $i * $base2;
}
while($i >= 1) {
if($answer < $i) {
$output .= "0";
}
else {
$fix = $answer%$i;
$fix = $answer - $fix;
$num = $fix/$i;
$answer -= ($i * $num);
if($num <= 9) {
$output .= $num;
}
if($num > 9 and $num <= 35) {
$num2 = $num + 55;
$output .= chr("$num2");
}
if($num > 35) {
$num2 = $num + 61;
$output .= chr("$num2");
}
}
$i = $i/$base2;
}
$out2 = "";
if(substr($output,0,1) eq "0") {
for($j = 1; $j<= length($output); $j++) {
$out2 .= substr($output,$j,1);
}
$ent3 -> delete('0.0', 'end');
$ent3 -> insert('end',$out2);
}
else {
$ent3 -> delete('0.0', 'end');
$ent3 -> insert('end',$output);
}
}
And there's an extremely ugly Perl TK version.