tkguifan has asked for the wisdom of the Perl Monks concerning the following question:

I want something like MIME::Base64, but only using lower case letters and numbers. In fact a base 32 encoding would be just fine ( which uses 32 of the 26 + 10 = 36 possible characters ). The aim is to turn hash keys into well behaved file names. Here is my solution:
my $string="foobar"; my $base32; my $bin=unpack 'B*',$string; print "string: $string\nbin: $bin\n"; print "original length of bin: ",length($bin),"\n"; my $bm5=length($bin) % 5; $bin=($bm5?('0'x(5-$bm5)):'').$bin; print "corrected length of bin: ",length($bin),"\n"; while($bin=~s/(.....)//) { my $dec=oct('0b'.$1); $base32.=$dec<26?chr(ord('a')+$dec):chr(ord('0')+$dec-26); } print "base32: $base32\n"; print "length of base32: ",length($base32),"\n";
prints:
string: foobar bin: 011001100110111101101111011000100110000101110010 original length of bin: 48 corrected length of bin: 50 base32: dgn3xweyls length of base32: 10

Replies are listed 'Best First'.
Re: How can I encode a string using only lower case letters and numbers?
by Perlbotics (Archbishop) on Feb 15, 2015 at 19:25 UTC

    Perhaps MIME::Base32?

    Author says:
    # RFC forces the [A-Z2-7] RFC-3548 compliant encoding
    # default encoding [0-9A-V] is for backward compatibility with pre v1.0
    Update: use MIME::Base32; (without the 'RFC') gives you the old behaviour (0-9A-V) if you want the 0-9 digit range... perhaps together with lc?
Re: How can I encode a string using only lower case letters and numbers?
by Corion (Patriarch) on Feb 15, 2015 at 19:35 UTC

    If your hash keys carry any meaning, I wrote Text::Fragment to turn "sentences" into sensible filenames. If you want to turn arbitrary hash keys into something sensible, I guess that Math::Fleximal is a good way, converting from base 256 to base 32 (with alphabet a-z0-9).

Re: How can I encode a string using only lower case letters and numbers?
by BrowserUk (Patriarch) on Feb 15, 2015 at 19:23 UTC
    In fact a base 32 encoding would be just fine ( which uses 32 of the 26 + 10 = 36 possible characters ).

    Why not Math::Base36 + tr[A-Z][a-z]?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      I have just installed Math::Base36, but it only accepts numbers, not strings. How do I turn a string into a decimal number?

        Will also deal with the full range of byte values, 0..255:

        $b36 = ''; $b36 .= encode_base36( $_, 7 ) for map unpack('V', $_), unpack '(Z4)*' +, 'ASillyLongstringWithFunnyCharacters!"£$%^&*():;@#~{[]}`¬¦|\/?';; print $b36;; 0U2W3EP0UVQCMK0WB704U0SP59PU0SZDAHJ0UN2HQE0R0Z2IH0WAKHV609A4RJ90AB0EG2 +0B56YQM0HTL7HL0PDSILF1B9UICD0D52TWD $s = ''; $s .= pack 'V', $_ for map decode_base36( $_ ), unpack '(a7)*', $b36;; print $s;; ASillyLongstringWithFunnyCharacters!"£$%^&*():;@#~{[]}`¬¦|\/

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: How can I encode a string using only lower case letters and numbers?
by ikegami (Patriarch) on Feb 16, 2015 at 02:11 UTC
Re: How can I encode a string using only lower case letters and numbers?
by Anonymous Monk on Feb 16, 2015 at 04:08 UTC

    Simple (and it's core)

    unpack 'H*', $string

    hehehe...