Base62 Class
Definition
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
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
charschar[]The character array.
offsetintThe starting offset.
countintThe number of characters.
styleBaseFormatStylesParsing 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 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
charsReadOnlySpan<char>The Base62 character span.
styleBaseFormatStylesParsing 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
sstringThe Base62 input.
styleBaseFormatStylesParsing styles. Only IgnoreWhitespace has effect.
Returns
- byte[]
The decoded byte array.
Exceptions
- ArgumentNullException
Thrown when
sis 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
bytesbyte[]The bytes to encode.
Returns
- string
A Base62 string.
Exceptions
- ArgumentNullException
Thrown when
bytesis 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
bytesbyte[]The byte array to encode.
offsetintThe starting offset.
countintThe number of bytes to encode.
Returns
- string
A Base62 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 Base62 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 the actual encoded length in size.
Returns
- int
The number of characters written.
Exceptions
- ArgumentException
Thrown when
destinationis 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
charCountintThe input character count.
Returns
- int
An upper bound on the decoded byte count.
Exceptions
- ArgumentOutOfRangeException
Thrown when
charCountis negative.
GetMaxEncodedLength(int)
Returns an upper bound on the number of characters required to encode byteCount bytes.
public static int GetMaxEncodedLength(int byteCount)
Parameters
byteCountintThe 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
byteCountis negative.
IsBase62Digit(char)
Indicates whether value is a valid Base62 digit.
public static bool IsBase62Digit(char value)
Parameters
valuecharThe character to test.
Returns
IsValid(ReadOnlySpan<char>, BaseFormatStyles)
Indicates whether source is a valid Base62 input.
public static bool IsValid(ReadOnlySpan<char> source, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The character span.
stylesBaseFormatStylesParsing styles. Only IgnoreWhitespace has effect.
Returns
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
charsReadOnlySpan<char>The Base62 character span.
destinationSpan<byte>The destination byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written, or
0on failure.styleBaseFormatStylesParsing styles.
Returns
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
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 |