uuencode table
The following table shows the conversion of the decimal value of the 6-bit fields obtained during the conversion process and their corresponding ASCII character output code and character.
Note that some encoders might produce space (code 32) instead of grave accent ("`", code 96), while some decoders might refuse to decode data containing space.
Example
The following is an example of uuencoding a one-line text file. In this example, %0D is the byte representation for carriage return, and %0A is the byte representation for line feed.
- file
File Name = wikipedia-url.txt
File Contents = http://www.wikipedia.org%0D%0A
- uuencoding
begin 644 wikipedia-url.txt
::'1T<#HO+W=W=RYW:6MI<&5D:6$N;W)G#0H`
`
end
Forks (file, resource)
Unix traditionally has a single fork where file data is stored. However, some file systems support multiple forks associated with a single file. For example, classic Mac OS Hierarchical File System (HFS) supported a data fork and a resource fork. Mac OS HFS+ supports multiple forks, as does Microsoft Windows NTFS alternate data streams. Most uucoding tools will only handle data from the primary data fork, which can result in a loss of information when encoding/decoding (for example, Windows NTFS file comments are kept in a different fork). Some tools (like the classic Mac OS application UUTool) solved the problem by concatenating the different forks into one file and differentiating them by file name.
Relation to xxencode, Base64, and Ascii85
Main articles: xxencoding, Base64, and Ascii85
Despite its limited range of characters, uuencoded data is sometimes corrupted on passage through certain computers using non-ASCII character sets such as EBCDIC. One attempt to solve the problem was the xxencode format, which used only alphanumeric characters and the plus and minus symbols. More common today is the Base64 format, which is based on the same concept of alphanumeric-only as opposed to ASCII 32–95. All three formats use 6 bits (64 different characters) to represent their input data.
Base64 can also be generated by the uuencode program and is similar in format, except for the actual character translation:
The header is changed to
begin-base64 <mode> <file>
the trailer becomes
====
and lines between are encoded with characters chosen from
ABCDEFGHIJKLMNOP
QRSTUVWXYZabcdef
ghijklmnopqrstuv
wxyz0123456789+/
Another alternative is Ascii85, which encodes four binary characters in five ASCII characters. Ascii85 is used in PostScript and PDF formats.
Disadvantages
uuencoding takes 3 pre-formatted bytes and turns them into 4 and also adds begin/end tags, filename, and delimiters. This adds at least 33% data overhead compared to the source alone, though this can be at least somewhat compensated for by compressing the file before uuencoding it.
Support in languages
Python
The Python language supports uuencoding using the codecs module with the codec "uu":
For Python 2 (deprecated/sunset as of January 1st 2020):
$ python -c 'print "Cat".encode("uu")'
begin 666 <data>
#0V%T
end
$
For Python 3 where the codecs module needs to be imported and used directly:
$ python3 -c "from codecs import encode;print(encode(b'Cat', 'uu'))"
b'begin 666 <data>\n#0V%T\n \nend\n'
$
To decode, pass the whole file:
$ python3 -c "from codecs import decode;print(decode(b'begin 666 <data>\n#0V%T\n \nend\n', 'uu'))"
b'Cat'
Perl
The Perl language supports uuencoding natively using the pack() and unpack() operators with the format string "u":
$ perl -e 'print pack("u","Cat")'
#0V%T
Decoding base64 with unpack can likewise be accomplished by translating the characters:
$ perl -e 'print unpack("u","#0V%T")'
Cat
To produce wellformed uuencoded files, you need to use modules,3 or a little bit more of code:4
Encode (oneliner)
$ perl -ple 'BEGIN{use File::Basename;$/=undef;$sn=basename($ARGV[0]);} $_= "begin 600 $sn\n".(pack "u", $_)."`\nend" if $_' /some/file/to_encode.gz
PHP
The PHP language has a native convert_uuencode() function:
$ php -r "echo convert_uuencode('Cat');"
#0V%T
`
Decoding is done with the corresponding convert_uudecode() function:
$ php -r "echo convert_uudecode('#0V%T');"
Cat
External links
- uuencode entry in POSIX.1-2008
- GNU-sharutils – open source suite of shar/unshar/uuencode/uudecode utilities
- UUDeview – open-source program to encode/decode Base64, BinHex, uuencode, xxencode, etc. for Unix/Windows/DOS
- UUENCODE-UUDECODE – open-source program to encode/decode created by Clem "Grandad" Dye
- StUU – open source fast UUDecoder for Macintosh by Stuart Cheshire
- UUENCODE-UUDECODE – free on-line UUEncoder and UUDecoder
- Java UUDecoder – open source Java library for decoding uuencoded (mail) attachments
- AN11229 – NXP application note: UUencoding for UART ISP