I got bored, and tried to write my own base converter (without having first checked CPAN where I would have found Math::BaseCalc which would do what you want just fine, I think). It runs into problems with large numbers (The md5 value listed in your post, for example).
But if anyone's interested in tinkering, I'll post what I came up with... I think I could fix it by using Math::BigInt, but haven't tried just yet.
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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|