Table of Contents

Base45 Class

Definition

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

Provides Base45 encoding and decoding of binary data as defined by RFC 9285 - the compact alphanumeric encoding used to carry binary payloads inside a QR code's Alphanumeric mode.

public static class Base45
Inheritance
Base45
Inherited Members

Examples

// Encode an arbitrary byte payload for inclusion in a QR code.
byte[] data = { 0x41, 0x42 };
string encoded = Base45.Encode(data);          // "BB8"

// Round-trip.
byte[] roundtrip = Base45.Decode(encoded);      // { 0x41, 0x42 }

Remarks

Special-purpose encodings. Base45 (RFC 9285) packs each pair of bytes into three characters from the QR-code Alphanumeric-mode alphabet (0-9, A-Z, and nine symbols) with no padding; a trailing odd byte becomes two characters. Base62 uses the GMP-style alphabet 0-9 A-Z a-z and big-integer division by 62, preserving leading zero bytes. Bech32 and Bech32m comprise a human-readable part, the 1 separator, 5-bit data groups, and a six-symbol checksum.

Base45 encodes each pair of input bytes as a base-45 number written in three characters, and a trailing odd byte as a two-character base-45 number. Unlike Base64, Base32, and Base16 it uses no padding. The 45-character alphabet is the Alphanumeric-mode subset of US-ASCII: the digits 0-9, the upper-case letters A-Z, and the symbols space, $, %, *, +, -, ., /, and :.

Because the space character is itself a Base45 symbol, whitespace skipping via IgnoreWhitespace applies only to tab, carriage return, and line feed - never to the space character. The decoder is strict per RFC 9285 §6: it rejects characters outside the alphabet, an encoded length whose value modulo three is one, and any three-character group that decodes to a value greater than 65535 (or any two-character group greater than 255).

Methods

Decode(char[], int, int, BaseFormatStyles)

Decodes a portion of a character array into a byte array.

public static byte[] Decode(char[] chars, int offset, int count, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

chars char[]

The character array.

offset int

The starting offset.

count int

The number of characters.

styles BaseFormatStyles

Parsing styles.

Returns

byte[]

The decoded byte array.

Exceptions

ArgumentNullException

Thrown when chars is null.

ArgumentOutOfRangeException

Thrown when offset or count is out of range.

ArgumentException

Thrown when the segment defined by offset and count exceeds the available range of chars.

FormatException

Thrown when the input is not valid Base45.

Decode(ReadOnlySpan<char>, BaseFormatStyles)

Decodes a Base45 character span into a byte array.

public static byte[] Decode(ReadOnlySpan<char> chars, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

chars ReadOnlySpan<char>

The Base45 character span.

styles BaseFormatStyles

Parsing styles.

Returns

byte[]

The decoded byte array. Returns Empty<T>() for empty input.

Exceptions

FormatException

Thrown when the input contains a character outside the alphabet, has an invalid length, or contains a group that decodes to a value outside the valid byte range.

Decode(string, BaseFormatStyles)

Decodes a Base45 string into a byte array.

public static byte[] Decode(string s, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

s string

The Base45 input.

styles BaseFormatStyles

Parsing styles. Only IgnoreWhitespace has effect, and it skips tab, carriage return, and line feed but never the space symbol.

Returns

byte[]

The decoded byte array.

Exceptions

ArgumentNullException

Thrown when s is null.

FormatException

Thrown when the input is not valid Base45.

Encode(byte[])

Encodes bytes into a Base45 string.

public static string Encode(byte[] bytes)

Parameters

bytes byte[]

The bytes to encode.

Returns

string

A Base45 string.

Exceptions

ArgumentNullException

Thrown when bytes is null.

Encode(byte[], int, int)

Encodes a portion of bytes into a Base45 string.

public static string Encode(byte[] bytes, int offset, int count)

Parameters

bytes byte[]

The byte array to encode.

offset int

The starting offset.

count int

The number of bytes to encode.

Returns

string

A Base45 string.

Exceptions

ArgumentNullException

Thrown when bytes is null.

ArgumentOutOfRangeException

Thrown when offset or count is out of range.

ArgumentException

Thrown when the segment defined by offset and count exceeds the available range of bytes.

Encode(ReadOnlySpan<byte>)

Encodes a span of bytes into a Base45 string.

public static string Encode(ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>

The bytes to encode.

Returns

string

A Base45 string. Returns Empty for empty input.

Encode(ReadOnlySpan<byte>, Span<char>)

Encodes a span of bytes directly into a destination character span.

public static int Encode(ReadOnlySpan<byte> bytes, Span<char> destination)

Parameters

bytes ReadOnlySpan<byte>

The bytes to encode.

destination Span<char>

The destination span; must be at least GetEncodedLength(int) characters.

Returns

int

The number of characters written.

Exceptions

ArgumentException

Thrown when destination is too small.

GetEncodedLength(int)

Returns the exact number of characters required to encode byteCount bytes.

public static int GetEncodedLength(int byteCount)

Parameters

byteCount int

The input byte count.

Returns

int

The encoded character count: three characters per byte pair plus two for a trailing odd byte.

Exceptions

ArgumentOutOfRangeException

Thrown when byteCount is negative.

GetMaxDecodedLength(int)

Returns an upper bound on the number of bytes that decoding charCount characters can produce.

public static int GetMaxDecodedLength(int charCount)

Parameters

charCount int

The input character count.

Returns

int

An upper bound on the decoded byte count.

Remarks

The returned value is exact for well-formed input (two bytes per three-character group plus one for a trailing two-character group). When charCount modulo three is one - a length the decoder rejects - the value still serves as a safe buffer size.

Exceptions

ArgumentOutOfRangeException

Thrown when charCount is negative.

GetMaxEncodedLength(int)

Returns the number of characters required to encode byteCount bytes. Because Base45 is a fixed-rate encoding this value is exact rather than an upper bound.

public static int GetMaxEncodedLength(int byteCount)

Parameters

byteCount int

The input byte count.

Returns

int

The encoded character count.

Exceptions

ArgumentOutOfRangeException

Thrown when byteCount is negative.

IsBase45Digit(char)

Indicates whether value is a character in the Base45 alphabet.

public static bool IsBase45Digit(char value)

Parameters

value char

The character to test.

Returns

bool

true when the character belongs to the Base45 alphabet.

IsValid(ReadOnlySpan<char>, BaseFormatStyles)

Indicates whether source is a valid Base45 input.

public static bool IsValid(ReadOnlySpan<char> source, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

source ReadOnlySpan<char>

The character span to validate.

styles BaseFormatStyles

Parsing styles. Only IgnoreWhitespace has effect, and it skips tab, carriage return, and line feed but never the space symbol.

Returns

bool

true when source would decode without error; otherwise false.

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

Attempts to decode a Base45 character span into a destination byte span without throwing.

public static bool TryDecode(ReadOnlySpan<char> chars, Span<byte> destination, out int bytesWritten, BaseFormatStyles styles = BaseFormatStyles.None)

Parameters

chars ReadOnlySpan<char>

The Base45 character span.

destination Span<byte>

The destination byte span.

bytesWritten int

When this method returns, contains the number of bytes written, or 0 on failure.

styles BaseFormatStyles

Parsing styles.

Returns

bool

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

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

Attempts to encode a span of bytes into a destination character span without throwing.

public static bool TryEncode(ReadOnlySpan<byte> bytes, Span<char> destination, out int charsWritten)

Parameters

bytes ReadOnlySpan<byte>

The bytes to encode.

destination Span<char>

The destination span.

charsWritten int

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

Returns

bool

true when the destination is large enough; otherwise false.

Applies to

ProductVersions
.NET8, 10