in reply to convert numbers to a different (arbitrary) base
So one time to take my mind off of being sick I wrote a module that makes it doable, Math::FlexibleMath::Fleximal. With that you can write the following:
Note that if you are using this to create strings to send to other people, you should consider removing various characters from the alphabet. Like the vowels, numbers that can be mistaken for being vowels (eg 3), and letters that can be mistaken for each other (eg 1 and l).use Math::Fleximal; my $number = '123456789012345678901234567890'; my $str = dec2alpha($number); print "$str\n"; print alpha2dec($str), "\n"; # Turns a number from base 10 to base 62 sub dec2alpha { Math::Fleximal->new( shift, [0..9] )->change_flex( [0..9, 'a'..'z', 'A'..'Z'] )->to_str(); } # Turns a number from base 62 to base 10 sub alpha2dec { Math::Fleximal->new( shift, [0..9, 'a'..'z', 'A'..'Z'] )->change_flex( [0..9] )->to_str(); } __DATA__ Prints: 2AyLS9BKAMjjsWHR0 123456789012345678901234567890
UPDATE: Corrected typo in module name. (Thanks tye.)
|
|---|