Table of Contents

Base62 Class

Definition

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

Provides Base62 encoding and decoding of binary data using the GMP-style alphabet 0-9 A-Z a-z.

public static class Base62
Inheritance
Base62
Inherited Members

Examples

byte[] data = { 0x00, 0xDE, 0xAD, 0xBE, 0xEF };
string encoded = Base62.Encode(data);          // leading zero byte -> leading '0'
byte[] roundtrip = Base62.Decode(encoded);

Remarks

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

Base62 is a non-power-of-two radix (radix 62) commonly used for compact, URL-safe identifiers and short links. Because the radix is not a power of two, encoding is performed with big-integer arithmetic rather than the bit-stream technique used by Base16, Base32, and Base64.

The alphabet orders digits first, then upper-case letters, then lower-case letters - the convention used by the GNU Multiple Precision library and most base-x implementations. Leading zero bytes in the input are encoded as leading 0 characters so that the byte-level and character-level forms preserve a meaningful prefix.

Base62 has no padding character and no standard decorations. Only IgnoreWhitespace affects decoding.

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 style = BaseFormatStyles.None)

Parameters

chars char[]

The character array.

offset int

The starting offset.

count int

The number of characters.

style 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 Base62.

Decode(ReadOnlySpan<char>, BaseFormatStyles)

Decodes a Base62 character span into a byte array.

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

Parameters

chars ReadOnlySpan<char>

The Base62 character span.

style BaseFormatStyles

Parsing styles.

Returns

byte[]

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

Exceptions

FormatException

Thrown when the input is not valid Base62.

Decode(string, BaseFormatStyles)

Decodes a Base62 string into a byte array.

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

Parameters

s string

The Base62 input.

style BaseFormatStyles

Parsing styles. Only IgnoreWhitespace has effect.

Returns

byte[]

The decoded byte array.

Exceptions

ArgumentNullException

Thrown when s is null.

FormatException

Thrown when the input contains characters outside the Base62 alphabet.

Encode(byte[])

Encodes bytes into a Base62 string.

public static string Encode(byte[] bytes)

Parameters

bytes byte[]

The bytes to encode.

Returns

string

A Base62 string.

Exceptions

ArgumentNullException

Thrown when bytes is null.

Encode(byte[], int, int)

Encodes a portion of bytes into a Base62 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 Base62 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 Base62 string.

public static string Encode(ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>

The bytes to encode.

Returns

string

A Base62 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 the actual encoded length in size.

Returns

int

The number of characters written.

Exceptions

ArgumentException

Thrown when destination is too small.

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.

Exceptions

ArgumentOutOfRangeException

Thrown when charCount is negative.

GetMaxEncodedLength(int)

Returns an upper bound on the number of characters required to encode byteCount bytes.

public static int GetMaxEncodedLength(int byteCount)

Parameters

byteCount int

The input byte count.

Returns

int

An upper bound on the encoded character count.

Remarks

Because Base62 is a non-power-of-two radix and the exact encoded length depends on the leading-zero count of the input, this overload returns a worst-case upper bound suitable for buffer sizing. The actual output length is the result of Encode(ReadOnlySpan<byte>) on the specific data.

Exceptions

ArgumentOutOfRangeException

Thrown when byteCount is negative.

IsBase62Digit(char)

Indicates whether value is a valid Base62 digit.

public static bool IsBase62Digit(char value)

Parameters

value char

The character to test.

Returns

bool

true when the character belongs to the Base62 alphabet.

IsValid(ReadOnlySpan<char>, BaseFormatStyles)

Indicates whether source is a valid Base62 input.

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

Parameters

source ReadOnlySpan<char>

The character span.

styles BaseFormatStyles

Parsing styles. Only IgnoreWhitespace has effect.

Returns

bool

true when the input is no longer than Bodu.Text.Encoding.Base62.MaxDecodeInputLength and every retained character is in the alphabet; otherwise false.

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

Attempts to decode a Base62 character span into a destination byte span.

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

Parameters

chars ReadOnlySpan<char>

The Base62 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.

style 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.

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