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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.