Skip to content

OnixLabs.Core.Text.Base16Codec

matthew edited this page May 27, 2026 · 1 revision

Base16Codec Class

Represents a codec for encoding and decoding Base-16 (hexadecimal) values.

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

Definition

public sealed class Base16Codec : IBaseCodec

Remarks

Base16Codec encodes using Convert.ToHexString and decodes using Convert.FromHexString. Its default provider is Base16FormatProvider.Invariant.

The casing of the encoded output depends on the provider: Base16FormatProvider.Uppercase produces uppercase output, while any other provider produces lowercase output. On decode, the casing rule is enforced: input containing lowercase letters is rejected under the Uppercase provider, and input containing uppercase letters is rejected under the Lowercase provider — in those cases TryDecode returns false (and Decode throws FormatException).

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-16 string.
Decode(ReadOnlySpan<char>, IFormatProvider?) byte[] Decodes the specified Base-16 value into a byte array.
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) bool Tries to encode the specified value into a Base-16 string.
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) bool Tries to decode the specified Base-16 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-16 string representation. See IBaseCodec.Encode for the full contract.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-16 string representation.
provider IFormatProvider? The Base16FormatProvider that controls casing. When null or a CultureInfo, defaults to Base16FormatProvider.Invariant (lowercase output).

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

Exceptions

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

Decode(ReadOnlySpan<char>, IFormatProvider?)

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

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

Parameter Type Description
value ReadOnlySpan<char> The Base-16 value to decode into a byte array.
provider IFormatProvider? The Base16FormatProvider that controls casing enforcement. When null or a CultureInfo, defaults to Base16FormatProvider.Invariant.

Returns — A new byte array decoded from value.

Exceptions

Exception Condition
FormatException value cannot be decoded, contains characters of the wrong case for the selected provider, or provider is not a Base16FormatProvider.

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-16 string representation. See IBaseCodec.TryEncode for the full contract.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-16 string representation.
provider IFormatProvider? The Base16FormatProvider that controls casing.
result out string When this method returns, contains the Base-16 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-16 representation into a byte array. See IBaseCodec.TryDecode for the full contract.

Parameter Type Description
value ReadOnlySpan<char> The Base-16 value to decode into a byte array.
provider IFormatProvider? The Base16FormatProvider that controls casing enforcement.
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 with uppercase and lowercase providers

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

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

string upper = codec.Encode(data, Base16FormatProvider.Uppercase);
string lower = codec.Encode(data, Base16FormatProvider.Lowercase);

Console.WriteLine(upper); // 48656C6C6F2C20576F726C6421
Console.WriteLine(lower); // 48656c6c6f2c20576f726c6421

Casing enforcement on decode

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.");
}

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

See Also

ONIXLabs .NET Library


Clone this wiki locally