convert_uuencode / convert_uudecode
convert_uuencode(string $string): string · convert_uudecode(string $string): string|false
Legacy uu encoding/decoding (length-prefixed lines, 3 bytes to 4 chars +32) — a 6bit scheme like base64 but with line structure and a ~ terminator; same encoding efficiency as base64 with more container overhead. Python's binascii matches byte for byte; other languages hand-roll it.
C C23
#include <stdio.h>
#include <string.h>
/* hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first */
static void uuencode(const unsigned char *in, size_t n, char *out) {
size_t k = 0;
out[k++] = (char)(n + 32);
for (size_t i = 0; i < n; i += 3) {
unsigned long grp = (unsigned long)in[i] << 16;
if (i + 1 < n) grp |= (unsigned long)in[i + 1] << 8;
if (i + 2 < n) grp |= in[i + 2];
out[k++] = (char)((grp >> 18 & 63) + 32);
out[k++] = (char)((grp >> 12 & 63) + 32);
out[k++] = (char)((grp >> 6 & 63) + 32);
out[k++] = (char)((grp & 63) + 32);
}
out[k] = '\0';
}
/* hand-rolled uudecode: length char, then 4 chars -> 3 bytes */
static int uudecode(const char *in, unsigned char *out) {
size_t len = (size_t)(in[0] - 32);
size_t k = 0;
for (size_t i = 1; i < 1 + (len + 2) / 3 * 4; i += 4) {
unsigned long grp = 0;
for (int j = 0; j < 4; j++)
grp = grp << 6 | (unsigned long)(in[i + j] - 32);
out[k++] = (unsigned char)(grp >> 16);
out[k++] = (unsigned char)(grp >> 8);
out[k++] = (unsigned char)grp;
}
return (int)len;
}
int main(void) {
const unsigned char data[] = "Hello UU!"; /* 9 bytes + NUL terminator */
char enc[32] = {0};
unsigned char dec[32] = {0};
uuencode(data, 9, enc);
printf("%s\n", enc);
int n = uudecode(enc, dec);
dec[n] = '\0';
printf("%s\n", (char *)dec);
return 0;
}
)2&5L;&\@554A Hello UU!
C++ C++26
#include <iostream>
#include <string>
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
static std::string uuencode(const std::string &in) {
std::string out;
out += (char)(in.size() + 32);
for (size_t i = 0; i < in.size(); i += 3) {
unsigned long grp = (unsigned char)in[i] << 16;
if (i + 1 < in.size()) grp |= (unsigned char)in[i + 1] << 8;
if (i + 2 < in.size()) grp |= (unsigned char)in[i + 2];
out += (char)((grp >> 18 & 63) + 32);
out += (char)((grp >> 12 & 63) + 32);
out += (char)((grp >> 6 & 63) + 32);
out += (char)((grp & 63) + 32);
}
return out;
}
// hand-rolled uudecode: length char, then 4 chars -> 3 bytes
static std::string uudecode(const std::string &in) {
size_t len = (size_t)(unsigned char)in[0] - 32;
std::string out;
for (size_t i = 1; i < 1 + (len + 2) / 3 * 4; i += 4) {
unsigned long grp = 0;
for (int j = 0; j < 4; j++)
grp = grp << 6 | (unsigned char)(in[i + j] - 32);
out += (char)(grp >> 16);
out += (char)(grp >> 8);
out += (char)grp;
}
return out.substr(0, len);
}
int main() {
std::string enc = uuencode("Hello UU!");
std::cout << enc << "\n";
std::cout << (uudecode(enc) == "Hello UU!" ? "roundtrip ok" : "fail") << "\n";
return 0;
}
)2&5L;&\@554A roundtrip ok
C# v11.0.100
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
static string Uuencode(byte[] input) {
var b = new System.Text.StringBuilder();
b.Append((char)(input.Length + 32));
for (int i = 0; i < input.Length; i += 3) {
ulong grp = (ulong)input[i] << 16;
if (i + 1 < input.Length) grp |= (ulong)input[i + 1] << 8;
if (i + 2 < input.Length) grp |= input[i + 2];
b.Append((char)((grp >> 18 & 63) + 32));
b.Append((char)((grp >> 12 & 63) + 32));
b.Append((char)((grp >> 6 & 63) + 32));
b.Append((char)((grp & 63) + 32));
}
return b.ToString();
}
static byte[] Uudecode(string line) {
int len = line[0] - 32;
var outBytes = new List<byte>();
for (int i = 1; i < 1 + (len + 2) / 3 * 4; i += 4) {
ulong grp = 0;
for (int j = 0; j < 4; j++)
grp = grp << 6 | (ulong)(line[i + j] - 32);
outBytes.Add((byte)(grp >> 16));
outBytes.Add((byte)(grp >> 8));
outBytes.Add((byte)grp);
}
return outBytes.Take(len).ToArray();
}
var data = System.Text.Encoding.UTF8.GetBytes("Hello UU!");
var enc = Uuencode(data);
Console.WriteLine(enc);
var dec = Uudecode(enc);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(dec) == "Hello UU!" ? "roundtrip ok" : "fail");
)2&5L;&\@554A roundtrip ok
Go v1.27.4
package main
import "fmt"
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
func uuencode(in []byte) string {
out := []byte{byte(len(in) + 32)}
for i := 0; i < len(in); i += 3 {
var grp uint32 = uint32(in[i]) << 16
if i+1 < len(in) {
grp |= uint32(in[i+1]) << 8
}
if i+2 < len(in) {
grp |= uint32(in[i+2])
}
out = append(out, byte(grp>>18&63+32), byte(grp>>12&63+32), byte(grp>>6&63+32), byte(grp&63+32))
}
return string(out)
}
// hand-rolled uudecode: length char, then 4 chars -> 3 bytes
func uudecode(line string) []byte {
length := int(line[0]) - 32
var out []byte
for i := 1; i < 1+(length+2)/3*4; i += 4 {
var grp uint32
for j := 0; j < 4; j++ {
grp = grp<<6 | uint32(line[i+j]-32)
}
out = append(out, byte(grp>>16), byte(grp>>8), byte(grp))
}
return out[:length]
}
func main() {
enc := uuencode([]byte("Hello UU!"))
fmt.Println(enc)
dec := uudecode(enc)
fmt.Println(string(dec) == "Hello UU!")
}
)2&5L;&\@554A true
Java v27.0.1
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class Main {
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
static String uuencode(byte[] in) {
StringBuilder b = new StringBuilder();
b.append((char) (in.length + 32));
for (int i = 0; i < in.length; i += 3) {
long grp = (in[i] & 0xFFL) << 16;
if (i + 1 < in.length) grp |= (long) (in[i + 1] & 0xFF) << 8;
if (i + 2 < in.length) grp |= in[i + 2] & 0xFF;
b.append((char) ((grp >> 18 & 63) + 32));
b.append((char) ((grp >> 12 & 63) + 32));
b.append((char) ((grp >> 6 & 63) + 32));
b.append((char) ((grp & 63) + 32));
}
return b.toString();
}
static byte[] uudecode(String line) {
int len = line.charAt(0) - 32;
List<Byte> out = new ArrayList<>();
for (int i = 1; i < 1 + (len + 2) / 3 * 4; i += 4) {
long grp = 0;
for (int j = 0; j < 4; j++)
grp = grp << 6 | (line.charAt(i + j) - 32);
out.add((byte) (grp >> 16));
out.add((byte) (grp >> 8));
out.add((byte) (long) grp);
}
byte[] arr = new byte[len];
for (int i = 0; i < len; i++) arr[i] = out.get(i);
return arr;
}
public static void main(String[] args) {
String enc = uuencode("Hello UU!".getBytes(StandardCharsets.UTF_8));
System.out.println(enc);
String dec = new String(uudecode(enc), StandardCharsets.UTF_8);
System.out.println(dec.equals("Hello UU!") ? "roundtrip ok" : "fail");
}
}
)2&5L;&\@554A roundtrip ok
JavaScript v26.13.1
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
function uuencode(bytes) {
let out = String.fromCharCode(bytes.length + 32);
for (let i = 0; i < bytes.length; i += 3) {
const b1 = bytes[i];
const b2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
const b3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
const n = (b1 << 16) | (b2 << 8) | b3;
out += String.fromCharCode(((n >> 18) & 63) + 32, ((n >> 12) & 63) + 32,
((n >> 6) & 63) + 32, (n & 63) + 32);
}
return out;
}
function uudecode(line) {
const len = line.charCodeAt(0) - 32;
const out = [];
for (let i = 1; i < 1 + ((len + 2) / 3 | 0) * 4; i += 4) {
let n = 0;
for (let j = 0; j < 4; j++) n = (n << 6) | (line.charCodeAt(i + j) - 32);
out.push((n >> 16) & 255, (n >> 8) & 255, n & 255);
}
return Buffer.from(out.slice(0, len));
}
const enc = uuencode(Buffer.from("Hello UU!"));
console.log(enc);
console.log(uudecode(enc).toString() === "Hello UU!" ? "roundtrip ok" : "fail");
)2&5L;&\@554A roundtrip ok
PHP v8.6.0
// uuencode: legacy 3-bytes-to-4-chars encoding with length-prefixed lines
$enc = convert_uuencode("Hello UU!");
echo $enc;
echo convert_uudecode($enc) === "Hello UU!" ? "roundtrip ok\n" : "fail\n";
echo strlen($enc), " chars for 9 bytes (line prefix + terminator overhead)\n";
)2&5L;&\@554A ` roundtrip ok 16 chars for 9 bytes (line prefix + terminator overhead)
Python v3.15.2
# binascii speaks the same uuencode as PHP (modulo the terminator line)
import binascii
enc = binascii.b2a_uu(b"Hello UU!")
print(enc.decode(), end="") # length char + data + newline, no ` terminator line
print("roundtrip ok" if binascii.a2b_uu(enc) == b"Hello UU!" else "fail")
print(len(enc), "chars for 9 bytes (PHP adds the ` line: 16)")
)2&5L;&\@554A roundtrip ok 14 chars for 9 bytes (PHP adds the ` line: 16)
Rust v1.99.1
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
fn uuencode(input: &[u8]) -> String {
let mut out = String::new();
out.push((input.len() as u8 + 32) as char);
let mut i = 0usize;
while i < input.len() {
let b1 = input[i] as u32;
let b2 = if i + 1 < input.len() { input[i + 1] as u32 } else { 0 };
let b3 = if i + 2 < input.len() { input[i + 2] as u32 } else { 0 };
let grp = (b1 << 16) | (b2 << 8) | b3;
out.push((((grp >> 18) & 63) + 32) as u8 as char);
out.push((((grp >> 12) & 63) + 32) as u8 as char);
out.push((((grp >> 6) & 63) + 32) as u8 as char);
out.push(((grp & 63) + 32) as u8 as char);
i += 3;
}
out
}
fn uudecode(line: &str) -> Vec<u8> {
let bytes: Vec<u8> = line.bytes().collect();
let len = (bytes[0] as usize) - 32;
let mut out = Vec::new();
let mut i = 1usize;
while i < 1 + (len + 2) / 3 * 4 {
let mut grp: u32 = 0;
for j in 0..4 {
grp = (grp << 6) | (bytes[i + j] as u32 - 32);
}
out.push((grp >> 16) as u8);
out.push((grp >> 8) as u8);
out.push(grp as u8);
i += 4;
}
out.truncate(len);
out
}
fn main() {
let enc = uuencode(b"Hello UU!");
println!("{}", enc);
let dec = uudecode(&enc);
println!("{}", if dec == b"Hello UU!" { "roundtrip ok" } else { "fail" });
}
)2&5L;&\@554A roundtrip ok
TypeScript v26.13.1
// hand-rolled uuencode: 3 bytes -> 4 chars (+32), length char first
function uuencode(bytes: Buffer): string {
let out = String.fromCharCode(bytes.length + 32);
for (let i = 0; i < bytes.length; i += 3) {
const b1 = bytes[i];
const b2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
const b3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
const n = (b1 << 16) | (b2 << 8) | b3;
out += String.fromCharCode(((n >> 18) & 63) + 32, ((n >> 12) & 63) + 32,
((n >> 6) & 63) + 32, (n & 63) + 32);
}
return out;
}
function uudecode(line: string): Buffer {
const len = line.charCodeAt(0) - 32;
const out: number[] = [];
for (let i = 1; i < 1 + ((len + 2) / 3 | 0) * 4; i += 4) {
let n = 0;
for (let j = 0; j < 4; j++) n = (n << 6) | (line.charCodeAt(i + j) - 32);
out.push((n >> 16) & 255, (n >> 8) & 255, n & 255);
}
return Buffer.from(out.slice(0, len));
}
const enc = uuencode(Buffer.from("Hello UU!"));
console.log(enc);
console.log(uudecode(enc).toString() === "Hello UU!" ? "roundtrip ok" : "fail");
)2&5L;&\@554A roundtrip ok
Language differences
C: hand-written encode/decode functions (example); uuencoding was the format of the UNIX uuencode/uudecode command-line tools, and a C implementation is still the best way to understand line-based encodings.
C++: hand-written encode/decode with string concatenation (example); no standard library counterpart.
C#: hand-written with ulong bit math (example); no built-in uu encoding — Convert.ToBase64String is the modern replacement (base64 semantics).
Go: hand-written with bit math (example); encoding/base64 covers modern use, uu encoding has no built-in support.
Java: hand-written with bit math (example); byte is signed so & 0xFF before shifting (a classic trap); the modern replacement is java.util.Base64.
JavaScript: hand-written with bit math plus Buffer (example); Buffer.toString('base64') is the modern replacement.
PHP: convert_uuencode output carries line structure — each line starts with a length character (raw byte count + 32), 60 characters per line, with a ` terminator line appended; 3 bytes to 4 chars is the same 6bit scheme as base64 (the same 4/3 expansion) but uu adds the length characters and terminator overhead; decoding failure returns false; a legacy mail-attachment encoding — use base64_encode for new code.
Python: binascii.b2a_uu/a2b_uu are the native uu pair — output is byte-identical to PHP's convert_uuencode (cross-language interop verified by execution); the stdlib uu module (whole-file wrapper) was removed in 3.13, so binascii is the recommended entry point.
Rust: hand-written with bit math (example); no standard library counterpart, and the crate ecosystem centers on base64.
TypeScript: same hand-written bit-math version as JavaScript (example).