Skip to content

OnixLabs.Core.Text.Base32

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

Base32 Struct

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

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

Definition

public readonly partial struct Base32 : IBaseValue<Base32>

Remarks

Base32 represents an arbitrary sequence of bytes whose canonical text form is Base-32. It is a thin value type over the underlying byte[]; the bytes are the data, and the Base-32 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 Base32 values are equal when their byte sequences are equal.

Encoding and decoding are delegated to the Base-32 codec exposed by Text (IBaseCodec.Base32). The Base-32 alphabet used for formatting and parsing, together with whether padding is applied, is supplied through an IFormatProvider, specifically a Base32FormatProvider. The library provides unpadded and padded variants of several well-known alphabets, including Rfc4648, ZBase32, GeoHash, Crockford, and Base32Hex.

Note The parameterless ToString() uses Base32FormatProvider.Rfc4648, the unpadded RFC 4648 alphabet.

Examples

Creating a Base32 value from bytes or text

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

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

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

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

Parsing a Base32 string

using OnixLabs.Core.Text;

Base32 parsed = Base32.Parse("JBSWY3DPFQQFO33SNRSCC===", Base32FormatProvider.PaddedRfc4648);

if (Base32.TryParse("JBSWY3DP", Base32FormatProvider.Rfc4648, out Base32 result))
{
    // result now holds the decoded bytes.
}

Formatting and converting to a string

using OnixLabs.Core.Text;

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

string rfc4648 = value.ToString();                                  // unpadded RFC 4648
string crockford = value.ToString(Base32FormatProvider.Crockford);  // Crockford alphabet
string padded = value.ToString(Base32FormatProvider.PaddedRfc4648); // padded RFC 4648

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

Constructors

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

Base32(ReadOnlySpan<byte>)

public Base32(ReadOnlySpan<byte> value)

Initializes a new instance of the Base32 struct.

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

Base32(ReadOnlyMemory<byte>)

public Base32(ReadOnlyMemory<byte> value)

Initializes a new instance of the Base32 struct.

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

Base32(ReadOnlySequence<byte>)

public Base32(ReadOnlySequence<byte> value)

Initializes a new instance of the Base32 struct.

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

Base32(ReadOnlySpan<char>, Encoding?)

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

Initializes a new instance of the Base32 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 Base32 instance.
encoding Encoding? The Encoding that will be used to transform the specified value, or null to use the default Encoding.

Base32(ReadOnlyMemory<char>, Encoding?)

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

Initializes a new instance of the Base32 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 Base32 instance.
encoding Encoding? The Encoding that will be used to transform the specified value, or null to use the default Encoding.

Base32(ReadOnlySequence<char>, Encoding?)

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

Initializes a new instance of the Base32 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 Base32 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?) Base32 Parses a Base-32 string into a Base32 value.
Parse(ReadOnlySpan<char>, IFormatProvider?) Base32 Parses a Base-32 character span into a Base32 value.
TryParse(string?, IFormatProvider?, out Base32) bool Attempts to parse a Base-32 string into a Base32 value.
TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base32) bool Attempts to parse a Base-32 character span into a Base32 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(Base32) bool Determines whether this value equals another Base32 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-32 representation using the unpadded RFC 4648 format provider.
ToString(IFormatProvider?) string Returns the Base-32 representation using the specified format provider.
ToString(string?, IFormatProvider?) string Returns the Base-32 representation using the specified format and format provider.
ToString(ReadOnlySpan<char>, IFormatProvider?) string Returns the Base-32 representation using the specified format and format provider.

Parse(string, IFormatProvider?)

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

Parses the specified Base-32 string into a Base32 value, decoding it with the Base-32 codec.

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

Returns — A Base32 value decoded from value.

Parse(ReadOnlySpan<char>, IFormatProvider?)

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

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

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

Returns — A Base32 value decoded from value.

TryParse(string?, IFormatProvider?, out Base32)

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

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

Parameter Type Description
value string? The Base-32 string to parse.
provider IFormatProvider? The format provider that supplies the Base-32 alphabet, or null to use the default.
result out Base32 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 Base32)

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

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

Parameter Type Description
value ReadOnlySpan<char> The Base-32 characters to parse.
provider IFormatProvider? The format provider that supplies the Base-32 alphabet, or null to use the default.
result out Base32 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(Base32)

public bool Equals(Base32 other)

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

Parameter Type Description
other Base32 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 Base32 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-32 representation of the current instance using Base32FormatProvider.Rfc4648 (the unpadded RFC 4648 alphabet).

Returns — The Base-32 encoded string.

ToString(IFormatProvider?)

public string ToString(IFormatProvider? formatProvider)

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

Parameter Type Description
formatProvider IFormatProvider? The provider that supplies the Base-32 alphabet and padding (typically a Base32FormatProvider).

Returns — The Base-32 encoded string.

ToString(string?, IFormatProvider?)

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

Returns the Base-32 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-32 alphabet and padding (typically a Base32FormatProvider).

Returns — The Base-32 encoded string.

ToString(ReadOnlySpan<char>, IFormatProvider?)

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

Returns the Base-32 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-32 alphabet and padding (typically a Base32FormatProvider).

Returns — The Base-32 encoded string.

Operators

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

operator ==

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

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

operator !=

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

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

implicit operator Base32(ReadOnlySpan<byte>)

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

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

implicit operator Base32(ReadOnlyMemory<byte>)

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

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

implicit operator Base32(ReadOnlySequence<byte>)

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

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

implicit operator Base32(ReadOnlySpan<char>)

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

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

implicit operator Base32(ReadOnlyMemory<char>)

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

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

implicit operator Base32(ReadOnlySequence<char>)

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

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

See Also

ONIXLabs .NET Library


Clone this wiki locally