Base45 Class
Definition
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
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
charschar[]The character array.
offsetintThe starting offset.
countintThe number of characters.
stylesBaseFormatStylesParsing styles.
Returns
- byte[]
The decoded byte array.
Exceptions
- ArgumentNullException
Thrown when
charsis null.- ArgumentOutOfRangeException
Thrown when
offsetorcountis out of range.- ArgumentException
Thrown when the segment defined by
offsetandcountexceeds the available range ofchars.- 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
charsReadOnlySpan<char>The Base45 character span.
stylesBaseFormatStylesParsing 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
sstringThe Base45 input.
stylesBaseFormatStylesParsing 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
sis 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
bytesbyte[]The bytes to encode.
Returns
- string
A Base45 string.
Exceptions
- ArgumentNullException
Thrown when
bytesis 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
bytesbyte[]The byte array to encode.
offsetintThe starting offset.
countintThe number of bytes to encode.
Returns
- string
A Base45 string.
Exceptions
- ArgumentNullException
Thrown when
bytesis null.- ArgumentOutOfRangeException
Thrown when
offsetorcountis out of range.- ArgumentException
Thrown when the segment defined by
offsetandcountexceeds the available range ofbytes.
Encode(ReadOnlySpan<byte>)
Encodes a span of bytes into a Base45 string.
public static string Encode(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
Returns
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
bytesReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span; must be at least GetEncodedLength(int) characters.
Returns
- int
The number of characters written.
Exceptions
- ArgumentException
Thrown when
destinationis too small.
GetEncodedLength(int)
Returns the exact number of characters required to encode byteCount bytes.
public static int GetEncodedLength(int byteCount)
Parameters
byteCountintThe 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
byteCountis 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
charCountintThe 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
charCountis 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
byteCountintThe input byte count.
Returns
- int
The encoded character count.
Exceptions
- ArgumentOutOfRangeException
Thrown when
byteCountis negative.
IsBase45Digit(char)
Indicates whether value is a character in the Base45 alphabet.
public static bool IsBase45Digit(char value)
Parameters
valuecharThe character to test.
Returns
IsValid(ReadOnlySpan<char>, BaseFormatStyles)
Indicates whether source is a valid Base45 input.
public static bool IsValid(ReadOnlySpan<char> source, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The character span to validate.
stylesBaseFormatStylesParsing styles. Only IgnoreWhitespace has effect, and it skips tab, carriage return, and line feed but never the space symbol.
Returns
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
charsReadOnlySpan<char>The Base45 character span.
destinationSpan<byte>The destination byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written, or
0on failure.stylesBaseFormatStylesParsing styles.
Returns
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
bytesReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span.
charsWrittenintWhen this method returns, contains the number of characters written.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |