Base64 Class
Definition
Provides Base64 encoding and decoding of binary data across the RFC 4648 standard, URL-safe, and MIME variants, with optional padding control and whitespace tolerance during parsing.
public static class Base64
- Inheritance
-
Base64
- Inherited Members
Examples
byte[] data = "hello"u8.ToArray();
// RFC 4648 Standard Base64 (default).
string standard = Base64.Encode(data); // "aGVsbG8="
// URL-safe alphabet, no padding - the form used by JWT and OAuth tokens.
string urlSafe = Base64.Encode(data, Base64Variant.UrlSafe); // "aGVsbG8"
// MIME variant - wraps every 76 characters with CRLF.
string mime = Base64.Encode(longerPayload, Base64Variant.Mime);
// Round-trip.
byte[] roundtrip = Base64.Decode(standard);
Remarks
The implementation delegates the inner radix conversion to Convert's ToBase64String(byte[], Base64FormattingOptions), TryToBase64Chars(ReadOnlySpan<byte>, Span<char>, out int, Base64FormattingOptions), and TryFromBase64Chars(ReadOnlySpan<char>, Span<byte>, out int) to inherit their hardware-accelerated paths. The wrapper supplies the alphabet swapping for UrlSafe, the line-break convention for Mime, and the padding / leniency flag handling that the BCL does not expose directly.
MIME line breaks are inserted every 76 characters (RFC 2045). The UpperCase, IncludePrefix, and InsertSpacing flags have no effect on Base64.
Methods
Decode(char[], int, int, Base64Variant, BaseFormatStyles)
Decodes a portion of a character array into a byte array.
public static byte[] Decode(char[] chars, int offset, int count, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
charschar[]The character array.
offsetintThe starting offset.
countintThe number of characters.
variantBase64VariantThe Base64 variant.
styleBaseFormatStylesParsing styles.
Returns
- byte[]
A new byte array containing the decoded data.
Exceptions
- ArgumentNullException
Thrown when
charsis null.- ArgumentOutOfRangeException
Thrown when
offsetorcountis out of range, or whenvariantis undefined.- ArgumentException
Thrown when the segment defined by
offsetandcountexceeds the available range ofchars.- FormatException
Thrown when the input is not valid Base64.
Decode(ReadOnlySpan<char>, Base64Variant, BaseFormatStyles)
Decodes a Base64 character span into a byte array.
public static byte[] Decode(ReadOnlySpan<char> chars, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
charsReadOnlySpan<char>The character span.
variantBase64VariantThe Base64 variant.
styleBaseFormatStylesParsing styles.
Returns
- byte[]
A new byte array containing the decoded data. Returns Empty<T>() when the input is empty.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.- FormatException
Thrown when the input is not valid Base64.
Decode(string, Base64Variant, BaseFormatStyles)
Decodes a Base64 string into a byte array using the supplied variant.
public static byte[] Decode(string s, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
sstringThe Base64 input string.
variantBase64VariantThe Base64 variant.
styleBaseFormatStylesParsing styles.
Returns
- byte[]
A new byte array containing the decoded data.
Exceptions
- ArgumentNullException
Thrown when
sis null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.- FormatException
Thrown when the input is not valid Base64 for the chosen variant and parsing styles.
DecodeFromUtf8(ReadOnlySpan<byte>, Span<byte>, out int, out int, Base64Variant, BaseFormatStyles, bool)
Decodes UTF-8 Base64 bytes into a byte span using the OperationStatus return convention.
public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles styles = BaseFormatStyles.None, bool isFinalBlock = true)
Parameters
sourceReadOnlySpan<byte>The UTF-8 Base64 source.
destinationSpan<byte>The destination span.
bytesConsumedintWhen this method returns, contains the number of source bytes consumed.
bytesWrittenintWhen this method returns, contains the number of bytes written.
variantBase64VariantThe variant.
stylesBaseFormatStylesParsing styles.
isFinalBlockboolWhether
sourcerepresents the final block.
Returns
- OperationStatus
An OperationStatus describing the outcome.
DecodeGuid(ReadOnlySpan<char>, Base64Variant, BaseFormatStyles)
Decodes a Base64 representation of a Guid.
public static Guid DecodeGuid(ReadOnlySpan<char> source, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The Base64 characters.
variantBase64VariantThe Base64 variant.
stylesBaseFormatStylesParsing styles.
Returns
Exceptions
- FormatException
Thrown when the input does not decode to exactly 16 bytes.
Encode(byte[], Base64Variant, BaseFormattingOptions)
Encodes the entire byte array into a Base64 string.
public static string Encode(byte[] bytes, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
bytesbyte[]The byte array to encode.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options. Only InsertLineBreaks and OmitPadding have an effect on Base64.
Returns
- string
The Base64 encoded string.
Exceptions
- ArgumentNullException
Thrown when
bytesis null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.
Encode(byte[], int, int, Base64Variant, BaseFormattingOptions)
Encodes a portion of a byte array into a Base64 string.
public static string Encode(byte[] bytes, int offset, int count, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
bytesbyte[]The byte array to encode.
offsetintThe starting offset.
countintThe number of bytes to encode.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options.
Returns
- string
The Base64 encoded string.
Exceptions
- ArgumentNullException
Thrown when
bytesis null.- ArgumentOutOfRangeException
Thrown when
offsetorcountis out of range, or whenvariantis undefined.- ArgumentException
Thrown when the segment defined by
offsetandcountexceeds the available range ofbytes.
Encode(Guid, Base64Variant, BaseFormattingOptions)
Encodes the byte representation of value as a Base64 string. With RFC 4648 padding the
result is 24 characters; without padding it is 22 characters.
public static string Encode(Guid value, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
valueGuidThe Guid to encode.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options.
Returns
- string
A Base64 string of the GUID bytes (mixed-endian, matching TryWriteBytes(Span<byte>)).
Encode(ReadOnlySpan<byte>, Base64Variant, BaseFormattingOptions)
Encodes a span of bytes into a Base64 string.
public static string Encode(ReadOnlySpan<byte> bytes, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options.
Returns
- string
The Base64 encoded string.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
Encode(ReadOnlySpan<byte>, IBufferWriter<char>, Base64Variant, BaseFormattingOptions)
Encodes source as Base64 characters into writer, suitable for use in
pipelines and other IBufferWriter<T>-based scenarios.
public static int Encode(ReadOnlySpan<byte> source, IBufferWriter<char> writer, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
writerIBufferWriter<char>The buffer writer that receives the encoded characters.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options. Only OmitPadding is supported.
Returns
- int
The number of characters written.
Exceptions
- ArgumentNullException
Thrown when
writeris null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.- ArgumentException
Thrown when
optionscontains an unsupported flag.
Encode(ReadOnlySpan<byte>, Span<char>, Base64Variant, BaseFormattingOptions)
Encodes a span of bytes directly into a destination character span.
public static int Encode(ReadOnlySpan<byte> bytes, Span<char> destination, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span. Must be at least GetEncodedLength(int, Base64Variant, BaseFormattingOptions) characters in size.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options.
Returns
- int
The number of characters written.
Exceptions
- ArgumentException
Thrown when
destinationis too small.- ArgumentOutOfRangeException
Thrown when
variantis undefined.
EncodeToUtf8(ReadOnlySpan<byte>, Base64Variant, BaseFormattingOptions)
Encodes source into a UTF-8 Base64 byte array.
public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options. Only OmitPadding has an effect on the UTF-8 fast path.
Returns
- byte[]
The UTF-8 encoded Base64 bytes.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.- ArgumentException
Thrown when
optionscontains any flag other than OmitPadding.
EncodeToUtf8(ReadOnlySpan<byte>, IBufferWriter<byte>, Base64Variant, BaseFormattingOptions)
Encodes source as UTF-8 Base64 bytes into writer, suitable for use in
pipelines and other IBufferWriter<T>-based scenarios.
public static int EncodeToUtf8(ReadOnlySpan<byte> source, IBufferWriter<byte> writer, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
writerIBufferWriter<byte>The buffer writer that receives the UTF-8 bytes.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options. Only OmitPadding is supported.
Returns
- int
The number of UTF-8 bytes written.
Exceptions
- ArgumentNullException
Thrown when
writeris null.- ArgumentOutOfRangeException
Thrown when
variantis undefined.- ArgumentException
Thrown when
optionscontains an unsupported flag.
FromBase64String(ReadOnlySpan<byte>)
Decodes UTF-8 Base64 bytes into a byte array using the Standard variant.
public static byte[] FromBase64String(ReadOnlySpan<byte> utf8Source)
Parameters
utf8SourceReadOnlySpan<byte>The UTF-8 Base64 source.
Returns
- byte[]
The decoded byte array.
Exceptions
- FormatException
Thrown when the input is not valid Standard Base64.
FromBase64String(ReadOnlySpan<byte>, Span<byte>, out int, out int)
Strict-mode Standard Base64 decode from a UTF-8 byte span.
public static OperationStatus FromBase64String(ReadOnlySpan<byte> utf8Source, Span<byte> destination, out int bytesConsumed, out int bytesWritten)
Parameters
utf8SourceReadOnlySpan<byte>The UTF-8 Base64 source.
destinationSpan<byte>The destination byte span.
bytesConsumedintWhen this method returns, contains the number of source bytes consumed.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
- OperationStatus
An OperationStatus describing the outcome.
FromBase64String(ReadOnlySpan<char>)
Decodes chars as a Standard Base64 character span into a byte array, mirroring the lenient
whitespace behaviour of FromBase64String(string).
public static byte[] FromBase64String(ReadOnlySpan<char> chars)
Parameters
charsReadOnlySpan<char>The Base64 character span.
Returns
- byte[]
The decoded byte array.
Exceptions
- FormatException
Thrown when the input is not valid Standard Base64.
FromBase64String(ReadOnlySpan<char>, Span<byte>, out int, out int)
Strict-mode Standard Base64 decode from a character span using the OperationStatus return convention.
public static OperationStatus FromBase64String(ReadOnlySpan<char> source, Span<byte> destination, out int charsConsumed, out int bytesWritten)
Parameters
sourceReadOnlySpan<char>The Base64 characters.
destinationSpan<byte>The destination byte span.
charsConsumedintWhen this method returns, contains the number of characters consumed.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
- OperationStatus
An OperationStatus describing the outcome.
FromBase64String(string)
Decodes s as a Standard Base64 string into a byte array, mirroring the lenient whitespace
behaviour of FromBase64String(string). ASCII whitespace anywhere in the input is
silently ignored; the canonical-padding rule and alphabet are otherwise enforced strictly.
public static byte[] FromBase64String(string s)
Parameters
sstringThe Base64 input.
Returns
- byte[]
The decoded byte array.
Remarks
To reject whitespace strictly, call Decode(string, Base64Variant, BaseFormatStyles) directly with None.
Exceptions
- ArgumentNullException
Thrown when
sis null.- FormatException
Thrown when the input is not valid Standard Base64.
GetDecodedLength(ReadOnlySpan<char>, Base64Variant, BaseFormatStyles)
Computes the exact number of bytes that decoding source would produce after applying
styles and stripping padding for the supplied variant.
public static int GetDecodedLength(ReadOnlySpan<char> source, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The Base64 character span.
variantBase64VariantThe Base64 variant.
stylesBaseFormatStylesParsing styles.
Returns
- int
The exact decoded byte count.
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.- FormatException
Thrown when the input contains invalid characters or the digit count is not a valid Base64 length.
GetEncodedLength(int)
Returns the number of characters produced by encoding byteCount bytes using the Standard
variant with default formatting.
public static int GetEncodedLength(int byteCount)
Parameters
byteCountintThe input byte count. Must be non-negative.
Returns
- int
The number of characters the encoder will produce.
Exceptions
- ArgumentOutOfRangeException
Thrown when
byteCountis negative.
GetEncodedLength(int, Base64Variant)
Returns the number of characters produced by encoding byteCount bytes using
variant with default formatting.
public static int GetEncodedLength(int byteCount, Base64Variant variant)
Parameters
byteCountintThe input byte count.
variantBase64VariantThe Base64 variant.
Returns
- int
The number of characters the encoder will produce.
Exceptions
- ArgumentOutOfRangeException
Thrown when
byteCountis negative orvariantis undefined.
GetEncodedLength(int, Base64Variant, BaseFormattingOptions)
Computes the number of characters required to encode byteCount bytes with the given variant
and options.
public static int GetEncodedLength(int byteCount, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
byteCountintThe number of input bytes. Must be non-negative.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsThe formatting options.
Returns
- int
The number of characters the matching encode overload will produce.
Exceptions
- ArgumentOutOfRangeException
Thrown when
byteCountis negative.
GetMaxDecodedLength(int)
Computes the maximum number of bytes that can result from decoding charCount characters.
public static int GetMaxDecodedLength(int charCount)
Parameters
charCountintThe number of input characters. Must be non-negative.
Returns
- int
The upper bound on the decoded byte count.
Exceptions
- ArgumentOutOfRangeException
Thrown when
charCountis negative.
IsBase64Digit(char, Base64Variant)
Indicates whether value is a valid Base64 alphabet character for the supplied variant.
Padding (=) is not considered a digit.
public static bool IsBase64Digit(char value, Base64Variant variant = Base64Variant.Standard)
Parameters
valuecharThe character to test.
variantBase64VariantThe variant.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
IsValid(ReadOnlySpan<char>, Base64Variant, BaseFormatStyles)
Indicates whether source is a valid Base64 input under the supplied variant and styles.
public static bool IsValid(ReadOnlySpan<char> source, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The character span.
variantBase64VariantThe variant.
stylesBaseFormatStylesParsing styles.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
ToBase64String(byte[])
Encodes inArray into a Standard Base64 string with default formatting.
public static string ToBase64String(byte[] inArray)
Parameters
inArraybyte[]The byte array to encode.
Returns
- string
A Base64 (RFC 4648 §4) string.
Exceptions
- ArgumentNullException
Thrown when
inArrayis null.
ToBase64String(byte[], int, int)
Encodes a portion of inArray into a Standard Base64 string.
public static string ToBase64String(byte[] inArray, int offset, int length)
Parameters
inArraybyte[]The byte array to encode.
offsetintThe starting offset.
lengthintThe number of bytes to encode.
Returns
- string
A Base64 string.
Exceptions
- ArgumentNullException
Thrown when
inArrayis null.- ArgumentOutOfRangeException
Thrown when
offsetorlengthis out of range.- ArgumentException
Thrown when the segment defined by
offsetandlengthexceeds the available range ofinArray.
ToBase64String(ReadOnlySpan<byte>)
Encodes bytes into a Standard Base64 string.
public static string ToBase64String(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
Returns
- string
A Base64 string.
TryDecode(ReadOnlySpan<char>, Span<byte>, out int, Base64Variant, BaseFormatStyles)
Attempts to decode Base64 characters into bytes using the provided destination span.
public static bool TryDecode(ReadOnlySpan<char> chars, Span<byte> destination, out int bytesWritten, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles style = BaseFormatStyles.None)
Parameters
charsReadOnlySpan<char>The input characters.
destinationSpan<byte>The destination byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written, or
0on failure.variantBase64VariantThe Base64 variant.
styleBaseFormatStylesParsing styles.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
TryDecodeGuid(ReadOnlySpan<char>, out Guid, Base64Variant, BaseFormatStyles)
Attempts to decode a Base64 representation of a Guid.
public static bool TryDecodeGuid(ReadOnlySpan<char> source, out Guid value, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The Base64 characters.
valueGuidWhen this method returns, contains the decoded Guid or Empty.
variantBase64VariantThe Base64 variant.
stylesBaseFormatStylesParsing styles.
Returns
TryEncode(ReadOnlySpan<byte>, Span<char>, out int, Base64Variant, BaseFormattingOptions)
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, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
bytesReadOnlySpan<byte>The bytes to encode.
destinationSpan<char>The destination span.
charsWrittenintWhen this method returns, contains the number of characters written, or
0when the destination is too small.variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
TryEncodeToUtf8(ReadOnlySpan<byte>, Span<byte>, out int, Base64Variant, BaseFormattingOptions)
Attempts to encode source as UTF-8 Base64 bytes into destination.
public static bool TryEncodeToUtf8(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, Base64Variant variant = Base64Variant.Standard, BaseFormattingOptions options = BaseFormattingOptions.None)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
destinationSpan<byte>The destination UTF-8 byte span.
bytesWrittenintWhen this method returns, contains the number of bytes written.
variantBase64VariantThe Base64 variant.
optionsBaseFormattingOptionsFormatting options. Only OmitPadding is supported.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.- ArgumentException
Thrown when unsupported flags are set in
options.
TryGetDecodedLength(ReadOnlySpan<char>, out int, Base64Variant, BaseFormatStyles)
Attempts to compute the exact decoded byte count for source.
public static bool TryGetDecodedLength(ReadOnlySpan<char> source, out int byteCount, Base64Variant variant = Base64Variant.Standard, BaseFormatStyles styles = BaseFormatStyles.None)
Parameters
sourceReadOnlySpan<char>The Base64 character span.
byteCountintWhen this method returns, contains the decoded byte count, or
0on failure.variantBase64VariantThe Base64 variant.
stylesBaseFormatStylesParsing styles.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
variantis undefined.
TryToBase64String(ReadOnlySpan<byte>, Span<byte>, out int)
Attempts to encode source as a Standard Base64 UTF-8 byte sequence into
utf8Destination.
public static bool TryToBase64String(ReadOnlySpan<byte> source, Span<byte> utf8Destination, out int bytesWritten)
Parameters
sourceReadOnlySpan<byte>The bytes to encode.
utf8DestinationSpan<byte>The UTF-8 destination span.
bytesWrittenintWhen this method returns, contains the number of bytes written.
Returns
TryToBase64String(ReadOnlySpan<byte>, Span<char>, out int)
Attempts to encode source into destination as a Standard Base64
character sequence.
public static bool TryToBase64String(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
Parameters
sourceReadOnlySpan<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 |