Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How do I decode a MIME/BASE64 string?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:32 UTC ( #772=perlfaq nodetype: print w/ replies, xml ) Need Help??

Current Perl documentation can be found at

Here is our local, out-dated (pre-5.6) version:

The MIME-tools package (available from CPAN) handles this and a lot more. Decoding BASE64 becomes as simple as:

    use MIME::base64;
    $decoded = decode_base64($encoded);

A more direct approach is to use the unpack() function's ``u'' format after minor transliterations:

    tr#A-Za-z0-9+/##cd;                   # remove non-base64 chars
    tr#A-Za-z0-9+/# -_#;                  # convert to uuencoded format
    $len = pack("c", 32 + 0.75*length);   # compute length byte
    print unpack("u", $len . $_);         # uudecode and print

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-07-29 10:09 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (213 votes), past polls