Skip to content

OnixLabs.Core.Text.Base64

Matthew Layton edited this page Nov 15, 2026 · 3 revisions

Base64 Struct

Represents a Base-64 encoded value: an immutable wrapper around a sequence of bytes whose textual representation is produced and consumed using a Base-64 alphabet.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ValueType → Base64
Implements IBaseValue&lt;Base64&gt; (and through it IValueEquatable<Base64>, ISpanParsable<Base64>, IBinaryConvertible, ISpanFormattable)

Definition

public readonly partial struct Base64 : IBaseValue<Base64>

Remarks

Base64 models a binary value together with its Base-64 textual form. Base-64 is a binary-to-text encoding that represents binary data using a 64-character alphabet, encoding three bytes of input as four characters of output and padding the final group with the = character as required. It is one of the most common encodings for transmitting binary data over text-oriented protocols.

The struct stores its underlying value as a defensive copy of the bytes supplied to its public constructors, which preserves value-type immutability. The encoding and decoding work is delegated to the Base-64 codec exposed through OnixLabs.Core.Text.Codecs; the alphabet used during encoding and decoding is selected by an IFormatProvider, namely a Base64FormatProvider (the supplied variant is Rfc4648).

Equality is based on value semantics: two Base64 instances are equal when their underlying byte sequences are equal. The default (parameterless) ToString() encodes the value using the default Base-64 alphabet.

Examples

Creating a Base64 value from bytes

using OnixLabs.Core.Text;

byte[] data = [1, 2, 3, 4];
Base64 value = new(data);

// Implicit conversion from a byte span is also supported.
Base64 converted = data.AsSpan();

Creating a Base64 value from text

using System.Text;
using OnixLabs.Core.Text;

// The characters are first encoded to bytes using the specified (or default) encoding,
// then wrapped as a Base64 value.
Base64 value = new("Hello, World!", Encoding.UTF8);

Formatting and parsing

using OnixLabs.Core.Text;

Base64 value = new(new byte[] { 0, 1, 2, 3 });

// Encode using the default alphabet.
string encoded = value.ToString();

// Encode using an explicit alphabet.
string rfc4648 = value.ToString(Base64FormatProvider.Rfc4648);

// Round-trip back to a Base64 value.
Base64 parsed = Base64.Parse(encoded);

if (Base64.TryParse(encoded, null, out Base64 result))
{
    // result is equal to value.
}

Reading the underlying bytes

Base64 value = new(new byte[] { 10, 20, 30 });

ReadOnlySpan<byte> span = value.AsReadOnlySpan();
ReadOnlyMemory<byte> memory = value.AsReadOnlyMemory();

Constructors

Constructor Description
Base64(ReadOnlySpan<byte>) Initializes a new instance from a read-only span of bytes.
Base64(ReadOnlyMemory<byte>) Initializes a new instance from a read-only memory region of bytes.
Base64(ReadOnlySequence<byte>) Initializes a new instance from a read-only sequence of bytes.
Base64(ReadOnlySpan<char>, Encoding?) Initializes a new instance from a read-only span of characters using the specified encoding.
Base64(ReadOnlyMemory<char>, Encoding?) Initializes a new instance from a read-only memory region of characters using the specified encoding.
Base64(ReadOnlySequence<char>, Encoding?) Initializes a new instance from a read-only sequence of characters using the specified encoding.

Base64(ReadOnlySpan<byte>)

public Base64(ReadOnlySpan<byte> value)

Initializes a new instance of the Base64 struct. A defensive copy of the supplied bytes is taken.

Parameter Type Description
value ReadOnlySpan<byte> The value from which to initialize the new Base64 instance.

Base64(ReadOnlyMemory<byte>)

public Base64(ReadOnlyMemory<byte> value)

Initializes a new instance of the Base64 struct. A defensive copy of the supplied bytes is taken.

Parameter Type Description
value ReadOnlyMemory<byte> The value from which to initialize the new Base64 instance.

Base64(ReadOnlySequence<byte>)

public Base64(ReadOnlySequence<byte> value)

Initializes a new instance of the Base64 struct. A defensive copy of the supplied bytes is taken.

Parameter Type Description
value ReadOnlySequence<byte> The value from which to initialize the new Base64 instance.

Base64(ReadOnlySpan<char>, Encoding?)

public Base64(ReadOnlySpan<char> value, Encoding? encoding = null)

Initializes a new instance of the Base64 struct from text. The characters are transformed into bytes using the specified encoding before being stored.

Parameter Type Description
value ReadOnlySpan<char> The value from which to initialize the new Base64 instance.
encoding Encoding? The Encoding used to transform the specified value, or null to use the default encoding.

Base64(ReadOnlyMemory<char>, Encoding?)

public Base64(ReadOnlyMemory<char> value, Encoding? encoding = null)

Initializes a new instance of the Base64 struct from text. The characters are transformed into bytes using the specified encoding before being stored.

Parameter Type Description
value ReadOnlyMemory<char> The value from which to initialize the new Base64 instance.
encoding Encoding? The Encoding used to transform the specified value, or null to use the default encoding.

Base64(ReadOnlySequence<char>, Encoding?)

public Base64(ReadOnlySequence<char> value, Encoding? encoding = null)

Initializes a new instance of the Base64 struct from text. The characters are transformed into bytes using the specified encoding before being stored.

Parameter Type Description
value ReadOnlySequence<char> The value from which to initialize the new Base64 instance.
encoding Encoding? The Encoding used to transform the specified value, or null to use the default encoding.

Methods

Static Methods

Method Returns Description
Parse(string, IFormatProvider?) Base64 Decodes the specified Base-64 string into a Base64 value.
Parse(ReadOnlySpan<char>, IFormatProvider?) Base64 Decodes the specified Base-64 character span into a Base64 value.
TryParse(string?, IFormatProvider?, out Base64) bool Attempts to decode the specified Base-64 string into a Base64 value.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base64) bool Attempts to decode the specified Base-64 character span into a Base64 value.

Instance Methods

Method Returns Description
AsReadOnlySpan() ReadOnlySpan<byte> Gets a read-only span over the underlying bytes.
AsReadOnlyMemory() ReadOnlyMemory<byte> Gets a read-only memory region over the underlying bytes.
Equals(Base64) bool Determines whether this value equals another Base64 value.
Equals(object?) bool Determines whether this value equals another object.
GetHashCode() int Returns a content-based hash code.
ToString() string Encodes the value using the default Base-64 alphabet.
ToString(IFormatProvider?) string Encodes the value using the specified format provider.
ToString(string?, IFormatProvider?) string Encodes the value using the specified format and format provider.
ToString(ReadOnlySpan<char>, IFormatProvider?) string Encodes the value using the specified format and format provider.

Parse(string, IFormatProvider?)

public static Base64 Parse(string value, IFormatProvider? provider = null)

Decodes the specified Base-64 string into a Base64 value.

Parameter Type Description
value string The Base-64 encoded string to decode.
provider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.

Returns — A new Base64 value decoded from value.

Parse(ReadOnlySpan<char>, IFormatProvider?)

public static Base64 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)

Decodes the specified Base-64 character span into a Base64 value.

Parameter Type Description
value ReadOnlySpan<char> The Base-64 encoded characters to decode.
provider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.

Returns — A new Base64 value decoded from value.

TryParse(string?, IFormatProvider?, out Base64)

public static bool TryParse(string? value, IFormatProvider? provider, out Base64 result)

Attempts to decode the specified Base-64 string into a Base64 value.

Parameter Type Description
value string? The Base-64 encoded string to decode.
provider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.
result out Base64 When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value.

Returns — true if value was decoded successfully; otherwise, false.

TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base64)

public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base64 result)

Attempts to decode the specified Base-64 character span into a Base64 value.

Parameter Type Description
value ReadOnlySpan<char> The Base-64 encoded characters to decode.
provider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.
result out Base64 When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value.

Returns — true if value was decoded successfully; otherwise, false.

AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

Gets a read-only span over the underlying bytes of the current Base64 value.

Returns — A ReadOnlySpan<byte> over the underlying value.

AsReadOnlyMemory()

public ReadOnlyMemory<byte> AsReadOnlyMemory()

Gets a read-only memory region over the underlying bytes of the current Base64 value.

Returns — A ReadOnlyMemory<byte> over the underlying value.

Equals(Base64)

public bool Equals(Base64 other)

Determines whether the current Base64 value is equal to another Base64 value by comparing their underlying byte sequences.

Returns — true if the values are equal; otherwise, false.

Equals(object?)

public override bool Equals(object? obj)

Determines whether the current Base64 value is equal to another object.

Returns — true if obj is a Base64 with an equal underlying value; otherwise, false.

GetHashCode()

public override int GetHashCode()

Returns a hash code derived from the content of the underlying byte sequence. Equal values produce equal hash codes; like HashCode, the result is randomised per process and must not be persisted.

Returns — A 32-bit signed integer hash code.

ToString()

public override string ToString()

Encodes the current value into its Base-64 string representation using the default Base-64 alphabet.

Returns — The Base-64 encoded string representation of the value.

ToString(IFormatProvider?)

public string ToString(IFormatProvider? formatProvider)

Encodes the current value into its Base-64 string representation using the alphabet supplied by the specified format provider.

Parameter Type Description
formatProvider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.

Returns — The Base-64 encoded string representation of the value.

ToString(string?, IFormatProvider?)

public string ToString(string? format, IFormatProvider? formatProvider)

Encodes the current value into its Base-64 string representation using the specified format and format provider.

Parameter Type Description
format string? The format to use.
formatProvider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.

Returns — The Base-64 encoded string representation of the value.

ToString(ReadOnlySpan<char>, IFormatProvider?)

public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvider)

Encodes the current value into its Base-64 string representation using the specified format and format provider.

Parameter Type Description
format ReadOnlySpan<char> The format to use.
formatProvider IFormatProvider? The format provider that supplies the Base-64 alphabet, or null to use the default.

Returns — The Base-64 encoded string representation of the value.

Operators

Operator Description
operator == Determines whether two Base64 values are equal.
operator != Determines whether two Base64 values are not equal.
implicit operator Base64(ReadOnlySpan<byte>) Converts a read-only span of bytes to a Base64 value.
implicit operator Base64(ReadOnlyMemory<byte>) Converts a read-only memory region of bytes to a Base64 value.
implicit operator Base64(ReadOnlySequence<byte>) Converts a read-only sequence of bytes to a Base64 value.
implicit operator Base64(ReadOnlySpan<char>) Converts a read-only span of characters to a Base64 value using the default encoding.
implicit operator Base64(ReadOnlyMemory<char>) Converts a read-only memory region of characters to a Base64 value using the default encoding.
implicit operator Base64(ReadOnlySequence<char>) Converts a read-only sequence of characters to a Base64 value using the default encoding.

operator ==

public static bool operator ==(Base64 left, Base64 right)

Performs an equality comparison between two Base64 values. Returns true if left is equal to right; otherwise, false.

operator !=

public static bool operator !=(Base64 left, Base64 right)

Performs an inequality comparison between two Base64 values. Returns true if left is not equal to right; otherwise, false.

Implicit conversions

public static implicit operator Base64(ReadOnlySpan<byte> value)
public static implicit operator Base64(ReadOnlyMemory<byte> value)
public static implicit operator Base64(ReadOnlySequence<byte> value)
public static implicit operator Base64(ReadOnlySpan<char> value)
public static implicit operator Base64(ReadOnlyMemory<char> value)
public static implicit operator Base64(ReadOnlySequence<char> value)

Implicitly converts the specified value into a new Base64 instance. The byte-based conversions wrap a defensive copy of the bytes; the character-based conversions first transform the characters into bytes using the default encoding.

See Also

ONIXLabs .NET Library


Clone this wiki locally