Skip to content

OnixLabs.Core.Text.Base16

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

Base16 Struct

Represents a Base-16 (hexadecimal) value: an immutable wrapper around a sequence of bytes that can be created from binary or text, parsed from a Base-16 string, and formatted back to a Base-16 string.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ValueType → Base16
Implements IBaseValue<Base16>, IValueEquatable<Base16>, ISpanParsable<Base16>, IBinaryConvertible, ISpanFormattable

Definition

public readonly partial struct Base16 : IBaseValue<Base16>

Remarks

Base16 represents an arbitrary sequence of bytes whose canonical text form is Base-16 (hexadecimal). It is a thin value type over the underlying byte[]; the bytes are the data, and the Base-16 alphabet only governs how those bytes are rendered to, and read back from, text.

The struct is immutable and has value semantics. The byte content is captured defensively on construction — the public constructors copy the supplied data — and equality is determined by comparing the byte content rather than the reference. Two Base16 values are equal when their byte sequences are equal.

Encoding and decoding are delegated to the Base-16 codec exposed by Text (IBaseCodec.Base16). The Base-16 alphabet used for formatting and parsing is supplied through an IFormatProvider, specifically a Base16FormatProvider (for example Base16FormatProvider.Invariant, Base16FormatProvider.Uppercase, or Base16FormatProvider.Lowercase).

Note The parameterless ToString() uses Base16FormatProvider.Invariant, which favours lowercase output and accepts both uppercase and lowercase hexadecimal digits when parsing.

Examples

Creating a Base16 value from bytes or text

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

// From a byte sequence.
byte[] bytes = [1, 2, 3, 4];
Base16 fromBytes = new(bytes);

// From text, using a specific encoding (defaults to the default encoding when null).
Base16 fromText = new("Hello, World!", Encoding.UTF8);

// Implicit conversion from a byte span or a character span.
Base16 implicitFromBytes = bytes.AsSpan();
Base16 implicitFromText = "Hello, World!".AsSpan();

Parsing a Base16 string

using OnixLabs.Core.Text;

Base16 parsed = Base16.Parse("48656c6c6f2c20576f726c6421");

if (Base16.TryParse("48656c6c6f", null, out Base16 result))
{
    // result now holds the decoded bytes.
}

Formatting and converting to a string

using OnixLabs.Core.Text;

Base16 value = new([0x48, 0x65, 0x6c, 0x6c, 0x6f]);

string invariant = value.ToString();                                  // "48656c6c6f"
string uppercase = value.ToString(Base16FormatProvider.Uppercase);    // "48656C6C6F"
string lowercase = value.ToString(Base16FormatProvider.Lowercase);    // "48656c6c6f"

// Access the underlying bytes without copying.
ReadOnlySpan<byte> span = value.AsReadOnlySpan();
ReadOnlyMemory<byte> memory = value.AsReadOnlyMemory();

Constructors

Constructor Description
Base16(ReadOnlySpan<byte>) Initializes a new instance of the Base16 struct from a span of bytes.
Base16(ReadOnlyMemory<byte>) Initializes a new instance of the Base16 struct from a memory of bytes.
Base16(ReadOnlySequence<byte>) Initializes a new instance of the Base16 struct from a sequence of bytes.
Base16(ReadOnlySpan<char>, Encoding?) Initializes a new instance of the Base16 struct from a span of characters.
Base16(ReadOnlyMemory<char>, Encoding?) Initializes a new instance of the Base16 struct from a memory of characters.
Base16(ReadOnlySequence<char>, Encoding?) Initializes a new instance of the Base16 struct from a sequence of characters.

Base16(ReadOnlySpan<byte>)

public Base16(ReadOnlySpan<byte> value)

Initializes a new instance of the Base16 struct.

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

Base16(ReadOnlyMemory<byte>)

public Base16(ReadOnlyMemory<byte> value)

Initializes a new instance of the Base16 struct.

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

Base16(ReadOnlySequence<byte>)

public Base16(ReadOnlySequence<byte> value)

Initializes a new instance of the Base16 struct.

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

Base16(ReadOnlySpan<char>, Encoding?)

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

Initializes a new instance of the Base16 struct from text. The characters are transformed into bytes using the specified encoding.

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

Base16(ReadOnlyMemory<char>, Encoding?)

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

Initializes a new instance of the Base16 struct from text. The characters are transformed into bytes using the specified encoding.

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

Base16(ReadOnlySequence<char>, Encoding?)

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

Initializes a new instance of the Base16 struct from text. The characters are transformed into bytes using the specified encoding.

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

Methods

Static Methods

Method Returns Description
Parse(string, IFormatProvider?) Base16 Parses a Base-16 string into a Base16 value.
Parse(ReadOnlySpan<char>, IFormatProvider?) Base16 Parses a Base-16 character span into a Base16 value.
TryParse(string?, IFormatProvider?, out Base16) bool Attempts to parse a Base-16 string into a Base16 value.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base16) bool Attempts to parse a Base-16 character span into a Base16 value.

Instance Methods

Method Returns Description
AsReadOnlySpan() ReadOnlySpan<byte> Gets the underlying bytes as a read-only span.
AsReadOnlyMemory() ReadOnlyMemory<byte> Gets the underlying bytes as a read-only memory.
Equals(Base16) bool Determines whether this value equals another Base16 value.
Equals(object?) bool Determines whether this value equals another object.
GetHashCode() int Returns a hash code derived from the byte content.
ToString() string Returns the Base-16 representation using the invariant format provider.
ToString(IFormatProvider?) string Returns the Base-16 representation using the specified format provider.
ToString(string?, IFormatProvider?) string Returns the Base-16 representation using the specified format and format provider.
ToString(ReadOnlySpan<char>, IFormatProvider?) string Returns the Base-16 representation using the specified format and format provider.

Parse(string, IFormatProvider?)

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

Parses the specified Base-16 string into a Base16 value, decoding it with the Base-16 codec.

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

Returns — A Base16 value decoded from value.

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Parses the specified Base-16 character span into a Base16 value, decoding it with the Base-16 codec.

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

Returns — A Base16 value decoded from value.

TryParse(string?, IFormatProvider?, out Base16)

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

Attempts to parse the specified Base-16 string into a Base16 value. When decoding fails, result is set to the default Base16 value.

Parameter Type Description
value string? The Base-16 string to parse.
provider IFormatProvider? The format provider that supplies the Base-16 alphabet, or null to use the default.
result out Base16 When this method returns, contains the parsed value on success, or the default value on failure.

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

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

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

Attempts to parse the specified Base-16 character span into a Base16 value. When decoding fails, result is set to the default Base16 value.

Parameter Type Description
value ReadOnlySpan<char> The Base-16 characters to parse.
provider IFormatProvider? The format provider that supplies the Base-16 alphabet, or null to use the default.
result out Base16 When this method returns, contains the parsed value on success, or the default value on failure.

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

AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

Gets the underlying byte content of the current instance as a read-only span.

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

AsReadOnlyMemory()

public ReadOnlyMemory<byte> AsReadOnlyMemory()

Gets the underlying byte content of the current instance as a read-only memory.

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

Equals(Base16)

public bool Equals(Base16 other)

Determines whether the current instance is equal to another Base16 value. Equality is based on the byte content of the two values.

Parameter Type Description
other Base16 The other value to compare against.

Returns — true if the byte content of both values is equal; otherwise, false.

Equals(object?)

public override bool Equals(object? obj)

Determines whether the current instance is equal to another object.

Returns — true if obj is a Base16 value with equal byte content; otherwise, false.

GetHashCode()

public override int GetHashCode()

Returns a hash code for the current instance, derived from its byte content. 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()

Returns the Base-16 representation of the current instance using Base16FormatProvider.Invariant.

Returns — The Base-16 encoded string.

ToString(IFormatProvider?)

public string ToString(IFormatProvider? formatProvider)

Returns the Base-16 representation of the current instance using the specified format provider.

Parameter Type Description
formatProvider IFormatProvider? The provider that supplies the Base-16 alphabet (typically a Base16FormatProvider).

Returns — The Base-16 encoded string.

ToString(string?, IFormatProvider?)

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

Returns the Base-16 representation of the current instance using the specified format and format provider.

Parameter Type Description
format string? The format to use.
formatProvider IFormatProvider? The provider that supplies the Base-16 alphabet (typically a Base16FormatProvider).

Returns — The Base-16 encoded string.

ToString(ReadOnlySpan<char>, IFormatProvider?)

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

Returns the Base-16 representation of the current instance using the specified format and format provider.

Parameter Type Description
format ReadOnlySpan<char> The format to use.
formatProvider IFormatProvider? The provider that supplies the Base-16 alphabet (typically a Base16FormatProvider).

Returns — The Base-16 encoded string.

Operators

Operator Description
operator == Determines whether two Base16 values are equal.
operator != Determines whether two Base16 values are not equal.
implicit operator Base16(ReadOnlySpan<byte>) Converts a byte span into a Base16 value.
implicit operator Base16(ReadOnlyMemory<byte>) Converts a byte memory into a Base16 value.
implicit operator Base16(ReadOnlySequence<byte>) Converts a byte sequence into a Base16 value.
implicit operator Base16(ReadOnlySpan<char>) Converts a character span into a Base16 value using the default encoding.
implicit operator Base16(ReadOnlyMemory<char>) Converts a character memory into a Base16 value using the default encoding.
implicit operator Base16(ReadOnlySequence<char>) Converts a character sequence into a Base16 value using the default encoding.

operator ==

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

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

operator !=

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

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

implicit operator Base16(ReadOnlySpan<byte>)

public static implicit operator Base16(ReadOnlySpan<byte> value)

Implicitly converts the specified byte span into a new Base16 instance.

implicit operator Base16(ReadOnlyMemory<byte>)

public static implicit operator Base16(ReadOnlyMemory<byte> value)

Implicitly converts the specified byte memory into a new Base16 instance.

implicit operator Base16(ReadOnlySequence<byte>)

public static implicit operator Base16(ReadOnlySequence<byte> value)

Implicitly converts the specified byte sequence into a new Base16 instance.

implicit operator Base16(ReadOnlySpan<char>)

public static implicit operator Base16(ReadOnlySpan<char> value)

Implicitly converts the specified character span into a new Base16 instance, using the default Encoding.

implicit operator Base16(ReadOnlyMemory<char>)

public static implicit operator Base16(ReadOnlyMemory<char> value)

Implicitly converts the specified character memory into a new Base16 instance, using the default Encoding.

implicit operator Base16(ReadOnlySequence<char>)

public static implicit operator Base16(ReadOnlySequence<char> value)

Implicitly converts the specified character sequence into a new Base16 instance, using the default Encoding.

See Also

ONIXLabs .NET Library


Clone this wiki locally