Skip to content

OnixLabs.Core.Text.Base64Codec

matthew edited this page May 27, 2026 · 1 revision

Base64Codec Class

Represents a codec for encoding and decoding Base-64 values.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Base64Codec
Implements IBaseCodec

Definition

public sealed class Base64Codec : IBaseCodec

Remarks

Base64Codec encodes using Convert.ToBase64String and decodes using Convert.FromBase64String. Its default and only supported provider is Base64FormatProvider.Rfc4648; the provider is used to validate that the codec was invoked with a compatible format provider rather than to vary the alphabet.

An empty input is treated as a successful no-op, producing an empty string (encoding) or an empty byte[] (decoding). Supplying a format provider of an incompatible type causes the Try* operations to return false and the Encode/Decode operations to throw a FormatException.

Methods

Method Returns Description
Encode(ReadOnlySpan<byte>, IFormatProvider?) string Encodes the specified value into a Base-64 string.
Decode(ReadOnlySpan<char>, IFormatProvider?) byte[] Decodes the specified Base-64 value into a byte array.
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) bool Tries to encode the specified value into a Base-64 string.
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) bool Tries to decode the specified Base-64 value into a byte array.

Encode(ReadOnlySpan<byte>, IFormatProvider?)

public string Encode(ReadOnlySpan<byte> value, IFormatProvider? provider = null);

Encodes the specified value into a Base-64 string representation. See IBaseCodec.Encode for the full contract.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-64 string representation.
provider IFormatProvider? The Base64FormatProvider to use. When null or a CultureInfo, defaults to Base64FormatProvider.Rfc4648.

Returns — A new Base-64 string representation encoded from value.

Exceptions

Exception Condition
FormatException value cannot be encoded, or provider is not a Base64FormatProvider.

Decode(ReadOnlySpan<char>, IFormatProvider?)

public byte[] Decode(ReadOnlySpan<char> value, IFormatProvider? provider = null);

Decodes the specified Base-64 representation into a byte array. See IBaseCodec.Decode for the full contract.

Parameter Type Description
value ReadOnlySpan<char> The Base-64 value to decode into a byte array.
provider IFormatProvider? The Base64FormatProvider to use. When null or a CultureInfo, defaults to Base64FormatProvider.Rfc4648.

Returns — A new byte array decoded from value.

Exceptions

Exception Condition
FormatException value is not a valid Base-64 string, or provider is not a Base64FormatProvider.

TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string)

public bool TryEncode(ReadOnlySpan<byte> value, IFormatProvider? provider, out string result);

Tries to encode the specified value into a Base-64 string representation. See IBaseCodec.TryEncode for the full contract.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-64 string representation.
provider IFormatProvider? The Base64FormatProvider to use.
result out string When this method returns, contains the Base-64 string encoded from value, or an empty string if the value could not be encoded.

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

TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[])

public bool TryDecode(ReadOnlySpan<char> value, IFormatProvider? provider, out byte[] result);

Tries to decode the specified Base-64 representation into a byte array. See IBaseCodec.TryDecode for the full contract.

Parameter Type Description
value ReadOnlySpan<char> The Base-64 value to decode into a byte array.
provider IFormatProvider? The Base64FormatProvider to use.
result out byte[] When this method returns, contains the byte array decoded from value, or an empty byte array if the value could not be decoded.

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

Examples

Encoding and decoding

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

byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
Base64Codec codec = new();

string encoded = codec.Encode(data);
byte[] decoded = codec.Decode(encoded);

Console.WriteLine(encoded);                              // SGVsbG8sIFdvcmxkIQ==
Console.WriteLine(Encoding.UTF8.GetString(decoded));     // Hello, World!

Dispatching via the static IBaseCodec helper

using System;
using OnixLabs.Core.Text;

byte[] data = [0xCA, 0xFE, 0xBA, 0xBE];

string encoded = IBaseCodec.GetString(data, Base64FormatProvider.Rfc4648);

if (IBaseCodec.TryGetBytes(encoded, Base64FormatProvider.Rfc4648, out byte[] decoded))
{
    Console.WriteLine(decoded.Length); // 4
}

See Also

ONIXLabs .NET Library


Clone this wiki locally