Repository navigation
OnixLabs.Core.Text.Base32
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
|
public readonly partial struct Base32 : IBaseValue<Base32>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()usesBase32FormatProvider.Rfc4648, the unpadded RFC 4648 alphabet.
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();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.
}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();| 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. |
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. |
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. |
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. |
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. |
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. |
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. |
| 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. |
| 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. |
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.
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.
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.
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.
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(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.
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.
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-32 representation of the current instance using Base32FormatProvider.Rfc4648 (the unpadded RFC 4648 alphabet).
Returns — The Base-32 encoded string.
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.
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.
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.
| 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. |
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.
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.
public static implicit operator Base32(ReadOnlySpan<byte> value)Implicitly converts the specified byte span into a new Base32 instance.
public static implicit operator Base32(ReadOnlyMemory<byte> value)Implicitly converts the specified byte memory into a new Base32 instance.
public static implicit operator Base32(ReadOnlySequence<byte> value)Implicitly converts the specified byte sequence into a new Base32 instance.
public static implicit operator Base32(ReadOnlySpan<char> value)Implicitly converts the specified character span into a new Base32 instance, using the default Encoding.
public static implicit operator Base32(ReadOnlyMemory<char> value)Implicitly converts the specified character memory into a new Base32 instance, using the default Encoding.
public static implicit operator Base32(ReadOnlySequence<char> value)Implicitly converts the specified character sequence into a new Base32 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