uuencode() function

The uuencode() function encodes a binary value as ASCII by using the UNIX UUencode format.

The encoding translates the binary value into ASCII character codes in the range 32 and above. Historically, uuencoding is used to encode files that are destined for email transmission. The uudecode() function reverses the effect of the uuencode() function, re-creating the original binary file exactly.

The uuencode() function algorithm does the following tasks:
  1. Divides the binary value into groups of three bytes (24 bits), adding zeros to the end of the binary value, if necessary, to create a final group of three bytes.
  2. Splits the 24 bits into four groups of six bits each. This creates four decimal numbers in the range 0 - 63.
  3. Adds decimal 32 to each number to create ASCII characters in the range 32 (space) - 95 (underscore).
Step 1 is illustrated by the following table that shows how each character of an input value, hat, is processed into bytes.
Table 1. uuencode() function algorithm tasks, Part I
Value type Input character Input character Input character
Original ASCII input h a t
ASCII decimal 104 97 116
ASCII binary (8 bits) 01101000 01100001 01110100

Steps 2 and 3 are illustrated by the following table. The transformation of the three 8-bit ASCII binary values in the preceding table to four 6-bit binary values is shown in the first line of the following table.

Table 2. uuencode() function algorithm tasks, Part II
Value type Input value Input value Input value Input value
6-bit binary 011010 000110 000101 110100
Decimal equivalent 26 6 5 52
Decimal + 32 58 38 37 84
Uuencoding : & % T

Syntax

The uuencode function has the following syntax:
varchar = uuencode(varchar input);
nvarchar = uuencode(nvarchar input);

The input value specifies the binary value to encode.

Returns

The function returns an encoded string.

For the uuencode() and uudecode() functions, the begin/end header information is important. If you remove the header information from the output of the uuencode() function and pass the encoded string to the uudecode() function, the uudecode() function returns ERROR: 0 : Invalid UUData.

Example

select uuencode ('hat');
      UUENCODE
--------------------
 begin
#:&%T
'
end

(1 row)