Skip to content

OnixLabs.Core.Text.Base32Codec

matthew edited this page May 27, 2026 · 1 revision

Base32Codec Class

Represents a codec for encoding and decoding Base-32 values.

Namespace OnixLabs.Core.Text
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Base32Codec
Implements IBaseCodec

Definition

public sealed class Base32Codec : IBaseCodec

Remarks

Base32Codec implements the standard 5-bit group Base-32 algorithm. The alphabet and padding behaviour are taken from the supplied Base32FormatProvider; its default provider is Base32FormatProvider.Rfc4648.

When the selected provider uses padding (see Base32FormatProvider.IsPadded), encoded output is padded with = characters to a multiple of eight characters, and decoding rejects input whose length is not a multiple of eight (TryDecode returns false). Decoding also fails if the input contains a character that is not part of the provider's alphabet.

An empty input is treated as a successful no-op, producing an empty string (encoding) or an empty byte[] (decoding). Supplying a format provider of an incompatible type causes the Try* operations to return false and the Encode/Decode operations to throw a FormatException.

Methods

Method Returns Description
Encode(ReadOnlySpan<byte>, IFormatProvider?) string Encodes the specified value into a Base-32 string.
Decode(ReadOnlySpan<char>, IFormatProvider?) byte[] Decodes the specified Base-32 value into a byte array.
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) bool Tries to encode the specified value into a Base-32 string.
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) bool Tries to decode the specified Base-32 value into a byte array.

Encode(ReadOnlySpan<byte>, IFormatProvider?)

public string Encode(ReadOnlySpan<byte> value, IFormatProvider? provider = null);

Encodes the specified value into a Base-32 string representation. See IBaseCodec.Encode for the full contract.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-32 string representation.
provider IFormatProvider? The Base32FormatProvider that supplies the alphabet and padding behaviour. When null or a CultureInfo, defaults to Base32FormatProvider.Rfc4648.

Returns — A new Base-32 string representation encoded from value.

Exceptions

Exception Condition
FormatException value cannot be encoded, or provider is not a Base32FormatProvider.

Decode(ReadOnlySpan<char>, IFormatProvider?)

public byte[] Decode(ReadOnlySpan<char> value, IFormatProvider? provider = null);

Decodes the specified Base-32 representation into a byte array. See IBaseCodec.Decode for the full contract.

Parameter Type Description
value ReadOnlySpan<char> The Base-32 value to decode into a byte array.
provider IFormatProvider? The Base32FormatProvider that supplies the alphabet and padding behaviour. When null or a CultureInfo, defaults to Base32FormatProvider.Rfc4648.

Returns — A new byte array decoded from value.

Exceptions

Exception Condition
FormatException value cannot be decoded, its length is not a multiple of eight when using a padded provider, it contains characters not in the provider's alphabet, or provider is not a Base32FormatProvider.

TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string)

public bool TryEncode(ReadOnlySpan<byte> value, IFormatProvider? provider, out string result);

Tries to encode the specified value into a Base-32 string representation. See IBaseCodec.TryEncode for the full contract.

Parameter Type Description
value ReadOnlySpan<byte> The value to encode into a Base-32 string representation.
provider IFormatProvider? The Base32FormatProvider that supplies the alphabet and padding behaviour.
result out string When this method returns, contains the Base-32 string encoded from value, or an empty string if the value could not be encoded.

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

TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[])

public bool TryDecode(ReadOnlySpan<char> value, IFormatProvider? provider, out byte[] result);

Tries to decode the specified Base-32 representation into a byte array. See IBaseCodec.TryDecode for the full contract.

Parameter Type Description
value ReadOnlySpan<char> The Base-32 value to decode into a byte array.
provider IFormatProvider? The Base32FormatProvider that supplies the alphabet and padding behaviour.
result out byte[] When this method returns, contains the byte array decoded from value, or an empty byte array if the value could not be decoded.

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

Examples

Encoding with different providers

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

byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
Base32Codec codec = new();

string rfc4648 = codec.Encode(data, Base32FormatProvider.Rfc4648);
string padded  = codec.Encode(data, Base32FormatProvider.PaddedRfc4648);
string zbase   = codec.Encode(data, Base32FormatProvider.ZBase32);

Console.WriteLine(rfc4648); // JBSWY3DPEB3W64TMMQQQ
Console.WriteLine(padded);  // JBSWY3DPEB3W64TMMQQQ====
Console.WriteLine(zbase);   // jb1sa5dxrb5ycodiiqqq

Round-tripping with a padded provider

using OnixLabs.Core.Text;

byte[] data = [0xDE, 0xAD, 0xBE, 0xEF];
Base32Codec codec = new();

string encoded = codec.Encode(data, Base32FormatProvider.PaddedRfc4648);
byte[] decoded = codec.Decode(encoded, Base32FormatProvider.PaddedRfc4648);
// decoded == [0xDE, 0xAD, 0xBE, 0xEF]

See Also

ONIXLabs .NET Library


Clone this wiki locally