PHP convert_uuencode() Function

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Functions

What You’ll Learn

The convert_uuencode() function encodes strings with the uuencode algorithm—a legacy binary-to-text format from Unix and early email. It turns raw bytes into printable ASCII and pairs with convert_uudecode() for decoding.

01

Encode to uuencode

Bytes → printable text.

02

One Parameter

Pass raw string data.

03

uuencode Pair

Decode with uudecode fn.

04

Binary Safe

Handles any byte value.

05

Legacy Format

Old email & Unix files.

06

vs Base64

Modern alternative.

Definition and Usage

In PHP, convert_uuencode() takes any string—text or binary—and returns a uuencoded representation made of printable ASCII characters. Each output line starts with a length character, followed by encoded payload. Data longer than 45 bytes is split across multiple lines automatically.

💡
Beginner Tip

For new applications, use base64_encode() instead of uuencode. Learn convert_uuencode() when you maintain legacy code, study encoding history, or need output compatible with old uuencode tools.

📝 Syntax

The convert_uuencode() function accepts one string parameter:

PHP
string convert_uuencode(string $string)

Parameters

  • $string — the data to encode. Can be plain text, binary bytes, or a mix of both.

Return Value

Returns the uuencoded string. Since PHP 8.0, encoding an empty string returns "" (previously it incorrectly returned false). The original input is not modified.

⚡ Quick Reference

Encode
convert_uuencode($data)

original → uuencode

Decode
convert_uudecode($encoded)

uuencode → original

Round-trip
convert_uudecode(convert_uuencode($s))

Should equal $s

Modern
base64_encode($data)

Base64 instead

Examples Gallery

Run these examples in PHP 7+. Each demonstrates a common pattern beginners encounter with convert_uuencode().

📚 Getting Started

Encode plain text into uuencode format.

Example 1 — Encode a Simple String

The most direct way to see uuencode in action: pass a text string and inspect the encoded output.

PHP
<?php
$data    = "Hello, PHP!";
$encoded = convert_uuencode($data);

echo "Original:  $data\n";
echo "UUEncoded: $encoded\n";
?>

How It Works

  • The leading + character encodes the line length (11 bytes in this case).
  • Following characters represent the encoded payload—all printable ASCII.
  • The trailing backtick (`) is padding for the final byte group.

Example 2 — Encode Multiline Text (PHP Manual Sample)

This is the official PHP documentation example—strings with embedded newlines encode cleanly.

PHP
<?php
$data    = "test\ntext text\r\n";
$encoded = convert_uuencode($data);

echo $encoded;
?>

How It Works

The first character 0 (ASCII 48) indicates a 16-byte line. You can verify the round-trip with convert_uudecode($encoded), which restores the original multiline string exactly.

📈 Practical Patterns

Binary data, size overhead, and modern alternatives.

Example 3 — Encode Binary Data

uuencode handles any byte value—including NUL (\x00) and high bytes—making it useful for binary payloads.

PHP
<?php
$binary  = "Binary\x00\xFFdata";
$encoded = convert_uuencode($binary);
$back    = convert_uudecode($encoded);

echo "Original (hex): " . bin2hex($binary) . "\n";
echo "Encoded:        $encoded\n";
echo "Round-trip OK:  " . ($back === $binary ? "yes" : "no") . "\n";
?>

How It Works

Even bytes that are not printable characters become safe ASCII after encoding. Use bin2hex() to inspect binary data when debugging.

Example 4 — Measure Encoding Size Overhead

uuencoded output is larger than the original—plan for extra storage or bandwidth when using this format.

PHP
<?php
$data    = "The quick brown fox jumps over the lazy dog";
$encoded = convert_uuencode($data);

$origLen = strlen($data);
$encLen  = strlen($encoded);
$overhead = round(($encLen / $origLen - 1) * 100);

echo "Original: $origLen bytes\n";
echo "Encoded:  $encLen bytes\n";
echo "Overhead: ~{$overhead}%\n";
?>

How It Works

PHP documentation notes uuencoded data is roughly 35% larger on average. Actual overhead varies with input length because of line-length prefixes and padding characters.

Example 5 — uuencode vs Base64 (Modern Alternative)

Compare legacy uuencode with Base64, the encoding standard used in modern web APIs and email.

PHP
<?php
$data = "Sample binary\xFF\xFE data";

$uu  = convert_uuencode($data);
$b64 = base64_encode($data);

echo "uuencode length:  " . strlen($uu) . "\n";
echo "Base64 length:    " . strlen($b64) . "\n";

echo "uudecode match:   " . (convert_uudecode($uu) === $data ? "yes\n" : "no\n");
echo "base64 match:     " . (base64_decode($b64) === $data ? "yes\n" : "no\n");
?>

How It Works

Both encodings represent binary as text, but Base64 is slightly more compact and universally supported today. Use base64_encode() for new code; use uuencode only for legacy compatibility.

🚀 Common Use Cases

  • Legacy email attachments — produce uuencoded sections compatible with old Unix mail systems.
  • Maintaining old PHP scripts — understand and extend code that still calls uuencode functions.
  • Binary-to-text transport — represent binary data as printable ASCII for text-only channels.
  • Round-trip testing — verify encoded output decodes correctly with convert_uudecode().
  • Learning encoding history — compare uuencode with modern Base64 approaches.

🧠 How convert_uuencode() Works

1

PHP reads your raw data

You pass a string containing text, binary bytes, or both—any byte values are accepted.

Input
2

Bytes are grouped and translated

PHP splits data into 45-byte chunks, encodes each group into printable ASCII characters, and prefixes each line with a length character.

Transform
3

uuencoded string is returned

The result contains only printable ASCII—safe for email, logs, and text-only storage.

Output
=

Printable encoded text

Ready to transmit, store, or decode with convert_uudecode().

📝 Notes

  • Output does not include begin or end wrapper lines found in standalone uuencode files.
  • Encoded data is roughly 35–45% larger than the original, depending on input length.
  • Data longer than 45 bytes is automatically split across multiple encoded lines.
  • For new projects, prefer base64_encode() / base64_decode().
  • uuencode and uudecode functions were deprecated in PHP 8.4; plan migrations for long-term PHP 9 compatibility.

Conclusion

The convert_uuencode() function converts raw data into the legacy uuencode format as printable ASCII. It remains useful for legacy compatibility and understanding historical PHP code, even though Base64 has replaced uuencode in modern applications.

Encode with convert_uuencode(), decode with convert_uudecode(), and reach for Base64 when building anything new.

💡 Best Practices

✅ Do

  • Verify round-trip with convert_uudecode() in tests
  • Use Base64 for all new binary-to-text encoding
  • Account for ~35% size increase when storing uuencoded data
  • Document when legacy uuencode output is required
  • Use bin2hex() to inspect binary input before encoding

❌ Don’t

  • Choose uuencode for new API or web application designs
  • Expect uuencode output to be human-readable content
  • Store binary in text columns when a BLOB field works
  • Assume full uuencode file headers are included automatically
  • Forget that PHP 8.4+ deprecates these functions

Key Takeaways

Knowledge Unlocked

Five things to remember about convert_uuencode()

Use these points whenever you encode data with uuencode in PHP.

5
Core concepts
🔄 02

uuencode Pair

Decode with uudecode.

Companion
🛠 03

Binary Safe

Any byte value works.

Syntax
📧 04

Legacy Email

Old Unix attachments.

Use case
05

Use Base64

For new projects.

Modern

❓ Frequently Asked Questions

convert_uuencode() encodes a string using the uuencode algorithm, converting binary or text data into printable ASCII characters safe for text-only channels like old email systems.
string convert_uuencode(string $string). You pass the raw data and receive a uuencoded string. Since PHP 8.0, encoding an empty string returns an empty string instead of false.
Both turn binary into text, but uuencode is a legacy Unix format (lines starting with length characters). Base64 is the modern standard—more compact, faster, and supported everywhere in web APIs and email today.
No. PHP encodes the data block only—it does not add the begin or end wrapper lines found in standalone uuencode files on disk.
Use it when maintaining legacy systems, reproducing old uuencode output, or learning historical encoding. For new PHP projects, prefer base64_encode() and base64_decode().
Use convert_uudecode() on the encoded string. A round-trip test—convert_uudecode(convert_uuencode($data))—should always equal the original $data.

Explore More PHP String Functions

Continue with count_chars(), explode(), and the rest of the string function reference.

Next: count_chars() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful