Skip to content

OnixLabs.Core.Text.Base58

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

Base58 Struct

Represents a Base-58 encoded value: an immutable wrapper around a sequence of bytes whose textual representation is produced and consumed using a Base-58 alphabet.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ValueType → Base58
Implements IBaseValue&lt;Base58&gt; (and through it IValueEquatable<Base58>, ISpanParsable<Base58>, IBinaryConvertible, ISpanFormattable)

Definition

public readonly partial struct Base58 : IBaseValue<Base58>

Remarks

Base58 models a binary value together with its Base-58 textual form. Base-58 is a binary-to-text encoding that uses 58 alphanumeric characters, omitting the visually ambiguous characters 0 (zero), O (capital o), I (capital i), and l (lower-case L), along with the + and / characters used by Base-64. It is widely used by cryptocurrency and distributed-system projects where human transcription of values is common.

The struct stores its underlying value as a defensive copy of the bytes supplied to its public constructors, which preserves value-type immutability. The encoding and decoding work is delegated to the Base-58 codec exposed through OnixLabs.Core.Text.Codecs; the specific alphabet used during encoding and decoding is selected by an IFormatProvider, namely a Base58FormatProvider (for example, Bitcoin, Flickr, or Ripple).

Note The alphabet variants supported by this type are alphabet substitutions only. They do not add or verify a checksum; this type does not implement the Base58Check variant used by some Bitcoin address formats.

Equality is based on value semantics: two Base58 instances are equal when their underlying byte sequences are equal. The default (parameterless) ToString() encodes the value using the Bitcoin alphabet.

Examples

Creating a Base58 value from bytes

using OnixLabs.Core.Text;

byte[] data = [1, 2, 3, 4];
Base58 value = new(data);

// Implicit conversion from a byte span is also supported.
Base58 converted = data.AsSpan();

Creating a Base58 value from text

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

// The characters are first encoded to bytes using the specified (or default) encoding,
// then wrapped as a Base58 value.
Base58 value = new("Hello, World!", Encoding.UTF8);

Formatting and parsing

using OnixLabs.Core.Text;

Base58 value = new(new byte[] { 0, 1, 2, 3 });

// Encode using the default (Bitcoin) alphabet.
string bitcoin = value.ToString();

// Encode using an explicit alphabet.
string flickr = value.ToString(Base58FormatProvider.Flickr);

// Round-trip back to a Base58 value.
Base58 parsed = Base58.Parse(bitcoin);

if (Base58.TryParse(bitcoin, null, out Base58 result))
{
    // result is equal to value.
}

Reading the underlying bytes

Base58 value = new(new byte[] { 10, 20, 30 });

ReadOnlySpan<byte> span = value.AsReadOnlySpan();
ReadOnlyMemory<byte> memory = value.AsReadOnlyMemory();

Constructors

Constructor Description
Base58(ReadOnlySpan<byte>) Initializes a new instance from a read-only span of bytes.
Base58(ReadOnlyMemory<byte>) Initializes a new instance from a read-only memory region of bytes.
Base58(ReadOnlySequence<byte>) Initializes a new instance from a read-only sequence of bytes.
Base58(ReadOnlySpan<char>, Encoding?) Initializes a new instance from a read-only span of characters using the specified encoding.
Base58(ReadOnlyMemory<char>, Encoding?) Initializes a new instance from a read-only memory region of characters using the specified encoding.
Base58(ReadOnlySequence<char>, Encoding?) Initializes a new instance from a read-only sequence of characters using the specified encoding.

Base58(ReadOnlySpan<byte>)

public Base58(ReadOnlySpan<byte> value)

Initializes a new instance of the Base58 struct. A defensive copy of the supplied bytes is taken.

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

Base58(ReadOnlyMemory<byte>)

public Base58(ReadOnlyMemory<byte> value)

Initializes a new instance of the Base58 struct. A defensive copy of the supplied bytes is taken.

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

Base58(ReadOnlySequence<byte>)

public Base58(ReadOnlySequence<byte> value)

Initializes a new instance of the Base58 struct. A defensive copy of the supplied bytes is taken.

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

Base58(ReadOnlySpan<char>, Encoding?)

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

Initializes a new instance of the Base58 struct from text. The characters are transformed into bytes using the specified encoding before being stored.

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

Base58(ReadOnlyMemory<char>, Encoding?)

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

Initializes a new instance of the Base58 struct from text. The characters are transformed into bytes using the specified encoding before being stored.

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

Base58(ReadOnlySequence<char>, Encoding?)

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

Initializes a new instance of the Base58 struct from text. The characters are transformed into bytes using the specified encoding before being stored.

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

Methods

Static Methods

Method Returns Description
Parse(string, IFormatProvider?) Base58 Decodes the specified Base-58 string into a Base58 value.
Parse(ReadOnlySpan<char>, IFormatProvider?) Base58 Decodes the specified Base-58 character span into a Base58 value.
TryParse(string?, IFormatProvider?, out Base58) bool Attempts to decode the specified Base-58 string into a Base58 value.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base58) bool Attempts to decode the specified Base-58 character span into a Base58 value.

Instance Methods

Method Returns Description
AsReadOnlySpan() ReadOnlySpan<byte> Gets a read-only span over the underlying bytes.
AsReadOnlyMemory() ReadOnlyMemory<byte> Gets a read-only memory region over the underlying bytes.
Equals(Base58) bool Determines whether this value equals another Base58 value.
Equals(object?) bool Determines whether this value equals another object.
GetHashCode() int Returns a content-based hash code.
ToString() string Encodes the value using the Bitcoin alphabet.
ToString(IFormatProvider?) string Encodes the value using the specified format provider.
ToString(string?, IFormatProvider?) string Encodes the value using the specified format and format provider.
ToString(ReadOnlySpan<char>, IFormatProvider?) string Encodes the value using the specified format and format provider.

Parse(string, IFormatProvider?)

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

Decodes the specified Base-58 string into a Base58 value.

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

Returns — A new Base58 value decoded from value.

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

Decodes the specified Base-58 character span into a Base58 value.

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

Returns — A new Base58 value decoded from value.

TryParse(string?, IFormatProvider?, out Base58)

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

Attempts to decode the specified Base-58 string into a Base58 value.

Parameter Type Description
value string? The Base-58 encoded string to decode.
provider IFormatProvider? The format provider that supplies the Base-58 alphabet, or null to use the default.
result out Base58 When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value.

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

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

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

Attempts to decode the specified Base-58 character span into a Base58 value.

Parameter Type Description
value ReadOnlySpan<char> The Base-58 encoded characters to decode.
provider IFormatProvider? The format provider that supplies the Base-58 alphabet, or null to use the default.
result out Base58 When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value.

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

AsReadOnlySpan()

public ReadOnlySpan<byte> AsReadOnlySpan()

Gets a read-only span over the underlying bytes of the current Base58 value.

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

AsReadOnlyMemory()

public ReadOnlyMemory<byte> AsReadOnlyMemory()

Gets a read-only memory region over the underlying bytes of the current Base58 value.

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

Equals(Base58)

public bool Equals(Base58 other)

Determines whether the current Base58 value is equal to another Base58 value by comparing their underlying byte sequences.

Returns — true if the values are equal; otherwise, false.

Equals(object?)

public override bool Equals(object? obj)

Determines whether the current Base58 value is equal to another object.

Returns — true if obj is a Base58 with an equal underlying value; otherwise, false.

GetHashCode()

public override int GetHashCode()

Returns a hash code derived from the content of the underlying byte sequence. 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()

Encodes the current value into its Base-58 string representation using the Bitcoin alphabet (Base58FormatProvider.Bitcoin).

Returns — The Base-58 encoded string representation of the value.

ToString(IFormatProvider?)

public string ToString(IFormatProvider? formatProvider)

Encodes the current value into its Base-58 string representation using the alphabet supplied by the specified format provider.

Parameter Type Description
formatProvider IFormatProvider? The format provider that supplies the Base-58 alphabet, or null to use the default.

Returns — The Base-58 encoded string representation of the value.

ToString(string?, IFormatProvider?)

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

Encodes the current value into its Base-58 string representation using the specified format and format provider.

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

Returns — The Base-58 encoded string representation of the value.

ToString(ReadOnlySpan<char>, IFormatProvider?)

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

Encodes the current value into its Base-58 string representation using the specified format and format provider.

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

Returns — The Base-58 encoded string representation of the value.

Operators

Operator Description
operator == Determines whether two Base58 values are equal.
operator != Determines whether two Base58 values are not equal.
implicit operator Base58(ReadOnlySpan<byte>) Converts a read-only span of bytes to a Base58 value.
implicit operator Base58(ReadOnlyMemory<byte>) Converts a read-only memory region of bytes to a Base58 value.
implicit operator Base58(ReadOnlySequence<byte>) Converts a read-only sequence of bytes to a Base58 value.
implicit operator Base58(ReadOnlySpan<char>) Converts a read-only span of characters to a Base58 value using the default encoding.
implicit operator Base58(ReadOnlyMemory<char>) Converts a read-only memory region of characters to a Base58 value using the default encoding.
implicit operator Base58(ReadOnlySequence<char>) Converts a read-only sequence of characters to a Base58 value using the default encoding.

operator ==

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

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

operator !=

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

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

Implicit conversions

public static implicit operator Base58(ReadOnlySpan<byte> value)
public static implicit operator Base58(ReadOnlyMemory<byte> value)
public static implicit operator Base58(ReadOnlySequence<byte> value)
public static implicit operator Base58(ReadOnlySpan<char> value)
public static implicit operator Base58(ReadOnlyMemory<char> value)
public static implicit operator Base58(ReadOnlySequence<char> value)

Implicitly converts the specified value into a new Base58 instance. The byte-based conversions wrap a defensive copy of the bytes; the character-based conversions first transform the characters into bytes using the default encoding.

See Also

ONIXLabs .NET Library


Clone this wiki locally