Doing this problem in pure Perl for small numbers is easy. Doing it in pure Perl for large numbers is hard because of round-off errors. In another language it could be easy - for instance Ruby by default uses infinite-length integers.

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:

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
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).

UPDATE: Corrected typo in module name. (Thanks tye.)


In reply to Re: convert numbers to a different (arbitrary) base by tilly
in thread convert numbers to a different (arbitrary) base by hostyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.