Table of Contents

QuotedPrintable Class

Definition

Namespace
Bodu.Text.Encoding
Assembly
Bodu.Text.Encoding.dll
Package
Bodu.Text.Encoding 1.0.0
Source
QuotedPrintable.Decode.cs

Provides MIME Quoted-Printable body encoding and decoding (RFC 2045 §6.7) of binary data, with selectable binary or canonical-text line handling, configurable soft line wrapping, and strict-by-default decoding.

public static class QuotedPrintable
Inheritance
QuotedPrintable
Inherited Members

Examples

byte[] data = "café = møney"u8.ToArray();

// Binary mode (default) - arbitrary octets, 76-column soft wrapping, CRLF.
string encoded = QuotedPrintable.Encode(data);

// Round-trip.
byte[] roundtrip = QuotedPrintable.Decode(encoded);

Remarks

Quoted-Printable represents mostly-7-bit-safe octets as readable text: printable ASCII passes through literally, while other octets are escaped as =HH with uppercase hexadecimal digits. Encoded lines are kept within a configurable limit (76 characters by default) by inserting soft line breaks - a trailing = followed by the newline - which the decoder removes.

Binary treats the whole input as arbitrary octets and escapes CR and LF; Text recognises canonical CRLF pairs as hard line breaks. Encoding always emits uppercase hex; decoding is strict by default but accepts lowercase hex, bare line feeds, and transport-inserted trailing whitespace under the corresponding QuotedPrintableDecodingOptions flags.

This type encodes and decodes only the Quoted-Printable body transform. It does not implement the RFC 2047 encoded-word Q header encoding (which has different syntax and underscore-for-space handling), and it does not parse MIME messages, headers, multiparts, charsets, or content-transfer-encoding declarations.

Because correct use depends on the mode and decoding options, this is a static type and is intentionally not registered in BinaryEncodings - the parameterless IBinaryEncoding contract cannot carry that information.

Methods

Decode(ReadOnlySpan<char>, QuotedPrintableDecodingOptions)

Decodes a Quoted-Printable character span into a byte array using the supplied options.

public static byte[] Decode(ReadOnlySpan<char> source, QuotedPrintableDecodingOptions options = QuotedPrintableDecodingOptions.None)

Parameters

source ReadOnlySpan<char>

The Quoted-Printable input.

options QuotedPrintableDecodingOptions

The decoding options.

Returns

byte[]

The decoded byte array. Returns an empty array for empty input.

Exceptions

FormatException

Thrown when the input is not well-formed Quoted-Printable.

Encode(ReadOnlySpan<byte>, QuotedPrintableEncodingOptions)

Encodes source into a Quoted-Printable string using the supplied options.

public static string Encode(ReadOnlySpan<byte> source, QuotedPrintableEncodingOptions options = default)

Parameters

source ReadOnlySpan<byte>

The bytes to encode.

options QuotedPrintableEncodingOptions

The encoding options.

Returns

string

A Quoted-Printable string.

Exceptions

ArgumentOutOfRangeException

Thrown when Mode is undefined or the normalized MaxLineLength is less than four.

ArgumentException

Thrown when NewLine is not "\r\n" or "\n".

GetEncodedLength(ReadOnlySpan<byte>, QuotedPrintableEncodingOptions)

Returns the exact number of characters that Encode(ReadOnlySpan<byte>, QuotedPrintableEncodingOptions) produces for the supplied data and options.

public static int GetEncodedLength(ReadOnlySpan<byte> source, QuotedPrintableEncodingOptions options = default)

Parameters

source ReadOnlySpan<byte>

The input bytes.

options QuotedPrintableEncodingOptions

The encoding options.

Returns

int

The exact encoded character count.

Exceptions

ArgumentOutOfRangeException

Thrown when Mode is undefined or the normalized MaxLineLength is less than four.

ArgumentException

Thrown when NewLine is not "\r\n" or "\n".

GetMaxDecodedLength(int)

Returns the maximum number of bytes that decoding charCount characters could produce.

public static int GetMaxDecodedLength(int charCount)

Parameters

charCount int

The input character count. Must be non-negative.

Returns

int

The worst-case decoded byte count, equal to charCount.

Exceptions

ArgumentOutOfRangeException

Thrown when charCount is negative.

GetMaxEncodedLength(int, QuotedPrintableEncodingOptions)

Returns an upper bound on the number of characters that encoding byteCount bytes could produce under the supplied options.

public static int GetMaxEncodedLength(int byteCount, QuotedPrintableEncodingOptions options = default)

Parameters

byteCount int

The input byte count. Must be non-negative.

options QuotedPrintableEncodingOptions

The encoding options.

Returns

int

The worst-case encoded character count.

Exceptions

ArgumentOutOfRangeException

Thrown when byteCount is negative, Mode is undefined, or the normalized MaxLineLength is less than four.

ArgumentException

Thrown when NewLine is not "\r\n" or "\n".

IsValid(ReadOnlySpan<char>, QuotedPrintableDecodingOptions)

Indicates whether source is canonical RFC 2045 Quoted-Printable under the supplied options, including the 76-character encoded-line limit.

public static bool IsValid(ReadOnlySpan<char> source, QuotedPrintableDecodingOptions options = QuotedPrintableDecodingOptions.None)

Parameters

source ReadOnlySpan<char>

The input characters.

options QuotedPrintableDecodingOptions

The decoding options.

Returns

bool

true when the input is canonical; otherwise false.

Remarks

This is stricter than Decode(ReadOnlySpan<char>, QuotedPrintableDecodingOptions), which performs lenient recovery and accepts encoded lines longer than 76 characters. IsValid enforces the fixed RFC 2045 limit (the soft-break = counted within it, the CRLF terminator not) regardless of any custom encode MaxLineLength. IsValid returning true implies Decode succeeds, but not the converse.

TryDecode(ReadOnlySpan<char>, Span<byte>, out int, QuotedPrintableDecodingOptions)

Attempts to decode a Quoted-Printable character span into a destination byte span.

public static bool TryDecode(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten, QuotedPrintableDecodingOptions options = QuotedPrintableDecodingOptions.None)

Parameters

source ReadOnlySpan<char>

The Quoted-Printable input.

destination Span<byte>

The destination byte span.

bytesWritten int

When this method returns, contains the number of bytes written.

options QuotedPrintableDecodingOptions

The decoding options.

Returns

bool

true on success; false when the input is malformed or the destination is too small.

TryEncode(ReadOnlySpan<byte>, Span<char>, out int, QuotedPrintableEncodingOptions)

Attempts to encode source into a destination character span.

public static bool TryEncode(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten, QuotedPrintableEncodingOptions options = default)

Parameters

source ReadOnlySpan<byte>

The bytes to encode.

destination Span<char>

The destination span.

charsWritten int

When this method returns, contains the number of characters written.

options QuotedPrintableEncodingOptions

The encoding options.

Returns

bool

true when the options are valid and the destination is large enough; otherwise false.

TryGetDecodedLength(ReadOnlySpan<char>, out int, QuotedPrintableDecodingOptions)

Attempts to determine the exact number of bytes that Decode(ReadOnlySpan<char>, QuotedPrintableDecodingOptions) will write for the supplied input.

public static bool TryGetDecodedLength(ReadOnlySpan<char> source, out int byteCount, QuotedPrintableDecodingOptions options = QuotedPrintableDecodingOptions.None)

Parameters

source ReadOnlySpan<char>

The input characters.

byteCount int

When this method returns, contains the decoded byte count, or 0 on failure.

options QuotedPrintableDecodingOptions

The decoding options.

Returns

bool

true when the input is decodable; otherwise false.

Remarks

This mirrors Decode(ReadOnlySpan<char>, QuotedPrintableDecodingOptions) (lenient recovery), not IsValid(ReadOnlySpan<char>, QuotedPrintableDecodingOptions) (canonical conformance): it does not enforce the 76-character line limit, so the result can size a decode buffer even for overlong input.

Applies to

ProductVersions
.NET8, 10