in reply to Compact MD5 representation

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; }

Replies are listed 'Best First'.
Re: Re: Compact MD5 representation
by paulbort (Hermit) on Mar 20, 2001 at 22:32 UTC
    You could use Math::BaseCalc (as suggested above) on the the first half and last half of the MD5, and then concatenate them. If you convert to base32 or base64, you wouldn't waste any bits. (And if you convert to base62 0-9a-zA-Z, you waste a couple bits, but it's portable just about everywhere.)