Skip to content

OnixLabs.Core.Text.IBaseCodec

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

IBaseCodec Interface

Defines a codec for encoding and decoding Base-N values.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Implemented by Base16Codec, Base32Codec, Base58Codec, Base64Codec

Definition

public interface IBaseCodec

Remarks

IBaseCodec is the unifying contract for the four Base-N codecs. It declares the four instance members every codec must provide — Encode, Decode, TryEncode, and TryDecode — and supplies a set of static convenience members.

The static factory properties (Base16, Base32, Base58, Base64) each return a shared codec instance: the same instance is returned on every access. The codecs are stateless and safe for concurrent use. The static GetString / GetBytes (and their Try counterparts) inspect the runtime type of the supplied IFormatProvider and dispatch to the matching codec, which makes them convenient when the encoding is selected solely by the format provider.

The Encode/Decode operations throw a FormatException on failure, whereas the Try* operations report failure through their bool return value and produce an empty result.

Every codec treats an empty input 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.

Static Properties

Property Type Description
Base16 Base16Codec Gets the shared Base16Codec instance.
Base32 Base32Codec Gets the shared Base32Codec instance.
Base58 Base58Codec Gets the shared Base58Codec instance.
Base64 Base64Codec Gets the shared Base64Codec instance.

Base16

public static Base16Codec Base16 { get; }

Gets the shared Base16Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.

Base32

public static Base32Codec Base32 { get; }

Gets the shared Base32Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.

Base58

public static Base58Codec Base58 { get; }

Gets the shared Base58Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.

Base64

public static Base64Codec Base64 { get; }

Gets the shared Base64Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.

Static Methods

Method Returns Description
GetString(ReadOnlySpan<byte>, IFormatProvider) string Obtains a Base-N string representation of the specified value, dispatching by provider.
GetBytes(ReadOnlySpan<char>, IFormatProvider) byte[] Obtains a new byte array by decoding the specified value, dispatching by provider.
TryGetString(ReadOnlySpan<byte>, IFormatProvider, out string) bool Tries to obtain a Base-N string representation of the specified value.
TryGetBytes(ReadOnlySpan<char>, IFormatProvider, out byte[]) bool Tries to obtain a new byte array by decoding the specified value.

GetString(ReadOnlySpan<byte>, IFormatProvider)

public static string GetString(ReadOnlySpan<byte> value, IFormatProvider provider)

Obtains a Base-N string representation of the specified value. The codec is selected from the runtime type of provider.

Parameter Type Description
value ReadOnlySpan<byte> The value to convert.
provider IFormatProvider The format provider used to obtain the base representation. Allowed values are Base16FormatProvider, Base32FormatProvider, Base58FormatProvider, and Base64FormatProvider.

Returns — A Base-N string representation of value.

Exceptions

Exception Condition
FormatException value cannot be converted, or provider is not one of the recognized Base-N format providers.

GetBytes(ReadOnlySpan<char>, IFormatProvider)

public static byte[] GetBytes(ReadOnlySpan<char> value, IFormatProvider provider)

Obtains a new byte array by decoding the specified Base-N value. The codec is selected from the runtime type of provider.

Parameter Type Description
value ReadOnlySpan<char> The value to convert.
provider IFormatProvider The format provider used to convert the value. Allowed values are Base16FormatProvider, Base32FormatProvider, Base58FormatProvider, and Base64FormatProvider.

Returns — A new byte array decoded from value.

Exceptions

Exception Condition
FormatException value cannot be converted, or provider is not one of the recognized Base-N format providers.

TryGetString(ReadOnlySpan<byte>, IFormatProvider, out string)

public static bool TryGetString(ReadOnlySpan<byte> value, IFormatProvider provider, out string result)

Tries to obtain a Base-N string representation of the specified value, dispatching to the codec that matches the runtime type of provider.

Parameter Type Description
value ReadOnlySpan<byte> The value to convert.
provider IFormatProvider The format provider used to obtain the base representation.
result out string When this method returns, contains the Base-N string representation of value, or an empty string if the value cannot be converted.

Returns — true if the conversion was successful; otherwise, false. Returns false when provider is not one of the recognized Base-N format providers.

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

public static bool TryGetBytes(ReadOnlySpan<char> value, IFormatProvider provider, out byte[] result)

Tries to obtain a new byte array by decoding the specified Base-N value, dispatching to the codec that matches the runtime type of provider.

Parameter Type Description
value ReadOnlySpan<char> The value to convert.
provider IFormatProvider The format provider used to convert the value.
result out byte[] When this method returns, contains the byte array decoded from value, or an empty byte array if the value cannot be converted.

Returns — true if the conversion was successful; otherwise, false. Returns false when provider is not one of the recognized Base-N format providers.

Instance Methods

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

Encode(ReadOnlySpan<byte>, IFormatProvider?)

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

Encodes the specified value into a Base-N string representation.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-N string representation.
provider IFormatProvider? The format provider that will be used to encode the value. When null, the codec's default provider is used.

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

Exceptions

Exception Condition
FormatException value cannot be encoded.

Decode(ReadOnlySpan<char>, IFormatProvider?)

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

Decodes the specified Base-N representation into a byte array.

Parameter Type Description
value ReadOnlySpan<char> The Base-N value to decode into a byte array.
provider IFormatProvider? The format provider that will be used to decode the value. When null, the codec's default provider is used.

Returns — A new byte array decoded from value.

Exceptions

Exception Condition
FormatException value cannot be decoded.

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

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

Tries to encode the specified value into a Base-N string representation.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-N string representation.
provider IFormatProvider? The format provider that will be used to encode the value.
result out string When this method returns, contains the Base-N 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[])

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

Tries to decode the specified Base-N representation into a byte array.

Parameter Type Description
value ReadOnlySpan<char> The Base-N value to decode into a byte array.
provider IFormatProvider? The format provider that will be used to decode the value.
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 with a codec instance

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

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

// Base-16 (hexadecimal), uppercase.
Base16Codec base16 = new();
string hex = base16.Encode(data, Base16FormatProvider.Uppercase);
byte[] fromHex = base16.Decode(hex, Base16FormatProvider.Uppercase);

// Base-58, Bitcoin alphabet (the default when provider is null).
Base58Codec base58 = new();
string b58 = base58.Encode(data);
byte[] fromB58 = base58.Decode(b58);

Console.WriteLine(hex);                              // 48656C6C6F2C20576F726C6421
Console.WriteLine(Encoding.UTF8.GetString(fromB58)); // Hello, World!

Using the static factory properties

using OnixLabs.Core.Text;

byte[] data = [0xDE, 0xAD, 0xBE, 0xEF];

string b32 = IBaseCodec.Base32.Encode(data, Base32FormatProvider.Rfc4648);
string b64 = IBaseCodec.Base64.Encode(data);

Dispatching by format provider

using System;
using OnixLabs.Core.Text;

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

// The codec is selected from the runtime type of the provider.
string encoded = IBaseCodec.GetString(data, Base64FormatProvider.Rfc4648);

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

Safe decoding with the Try pattern

using System;
using OnixLabs.Core.Text;

Base16Codec codec = new();

// Lowercase input is rejected under the uppercase provider.
if (!codec.TryDecode("deadbeef", Base16FormatProvider.Uppercase, out byte[] _))
{
    Console.WriteLine("Casing did not match the uppercase format provider.");
}

See Also

ONIXLabs .NET Library


Clone this wiki locally