sub convert_base { my($input_string,$from_base,$to_base) = @_; my($working_number,$current_digit,$i); my($quotient,converted_number); # # Convert input string to dec. # for ( $i = 0; $i < length($input_string); $i++ ) { $current_digit = uc(substr($input_string,$i,1)); if ( $current_digit !~ m/[0-9]/ ) { $current_digit = ord($current_digit) - ord("A") + 10; } $working_number += $current_digit * ($from_base ** (length($input_string) - $i - 1)); } # Figure out dec value of this digit. # # Convert int to new base. # while ( $quotient = int($working_number / $to_base) ) { $current_digit = $working_number % $to_base; if ( $current_digit > 9 ) { $current_digit = chr($current_digit - 10 + ord("A")); } $converted_number = $current_digit . $converted_number; $working_number = $quotient; } # # We've dropped out because there's only a bit of remainder left. # $current_digit = $working_number; if ( $current_digit > 9 ) { $current_digit = chr($current_digit - 10 + ord("A")); } $converted_number = $current_digit . $converted_number; return $converted_number; }