Repository navigation
OnixLabs.Core.Text.Base16
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
|
public readonly partial struct Base16 : IBaseValue<Base16>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()usesBase16FormatProvider.Invariant, which favours lowercase output and accepts both uppercase and lowercase hexadecimal digits when parsing.
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();using OnixLabs.Core.Text;
Base16 parsed = Base16.Parse("48656c6c6f2c20576f726c6421");
if (Base16.TryParse("48656c6c6f", null, out Base16 result))
{
// result now holds the decoded bytes.
}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();| 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. |
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. |
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. |
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. |
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. |
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. |
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. |
| 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. |
| 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. |
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.
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.
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.
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.
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.
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.
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.
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.
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.
public override string ToString()Returns the Base-16 representation of the current instance using Base16FormatProvider.Invariant.
Returns — The Base-16 encoded string.
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.
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.
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.
| 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. |
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.
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.
public static implicit operator Base16(ReadOnlySpan<byte> value)Implicitly converts the specified byte span into a new Base16 instance.
public static implicit operator Base16(ReadOnlyMemory<byte> value)Implicitly converts the specified byte memory into a new Base16 instance.
public static implicit operator Base16(ReadOnlySequence<byte> value)Implicitly converts the specified byte sequence into a new Base16 instance.
public static implicit operator Base16(ReadOnlySpan<char> value)Implicitly converts the specified character span into a new Base16 instance, using the default Encoding.
public static implicit operator Base16(ReadOnlyMemory<char> value)Implicitly converts the specified character memory into a new Base16 instance, using the default Encoding.
public static implicit operator Base16(ReadOnlySequence<char> value)Implicitly converts the specified character sequence into a new Base16 instance, using the default Encoding.
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions