Repository navigation
OnixLabs.Core.Text.IBaseCodec
Defines a codec for encoding and decoding Base-N values.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Implemented by |
Base16Codec, Base32Codec, Base58Codec, Base64Codec
|
public interface IBaseCodecIBaseCodec is the unifying contract for the four Base-N codecs. It declares the four instance members every codec must provide — Encode, Decode, TryEncode, and TryDecode — and supplies a set of static convenience members.
The static factory properties (Base16, Base32, Base58, Base64) each return a shared codec instance: the same instance is returned on every access. The codecs are stateless and safe for concurrent use. The static GetString / GetBytes (and their Try counterparts) inspect the runtime type of the supplied IFormatProvider and dispatch to the matching codec, which makes them convenient when the encoding is selected solely by the format provider.
The Encode/Decode operations throw a FormatException on failure, whereas the Try* operations report failure through their bool return value and produce an empty result.
Every codec treats an empty input 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.
| Property | Type | Description |
|---|---|---|
Base16 |
Base16Codec |
Gets the shared Base16Codec instance. |
Base32 |
Base32Codec |
Gets the shared Base32Codec instance. |
Base58 |
Base58Codec |
Gets the shared Base58Codec instance. |
Base64 |
Base64Codec |
Gets the shared Base64Codec instance. |
public static Base16Codec Base16 { get; }Gets the shared Base16Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.
public static Base32Codec Base32 { get; }Gets the shared Base32Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.
public static Base58Codec Base58 { get; }Gets the shared Base58Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.
public static Base64Codec Base64 { get; }Gets the shared Base64Codec instance, which is stateless and safe for concurrent use. Every access returns the same instance.
| Method | Returns | Description |
|---|---|---|
GetString(ReadOnlySpan<byte>, IFormatProvider) |
string |
Obtains a Base-N string representation of the specified value, dispatching by provider. |
GetBytes(ReadOnlySpan<char>, IFormatProvider) |
byte[] |
Obtains a new byte array by decoding the specified value, dispatching by provider. |
TryGetString(ReadOnlySpan<byte>, IFormatProvider, out string) |
bool |
Tries to obtain a Base-N string representation of the specified value. |
TryGetBytes(ReadOnlySpan<char>, IFormatProvider, out byte[]) |
bool |
Tries to obtain a new byte array by decoding the specified value. |
public static string GetString(ReadOnlySpan<byte> value, IFormatProvider provider)Obtains a Base-N string representation of the specified value. The codec is selected from the runtime type of provider.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to convert. |
provider |
IFormatProvider |
The format provider used to obtain the base representation. Allowed values are Base16FormatProvider, Base32FormatProvider, Base58FormatProvider, and Base64FormatProvider. |
Returns — A Base-N string representation of value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be converted, or provider is not one of the recognized Base-N format providers. |
public static byte[] GetBytes(ReadOnlySpan<char> value, IFormatProvider provider)Obtains a new byte array by decoding the specified Base-N value. The codec is selected from the runtime type of provider.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The value to convert. |
provider |
IFormatProvider |
The format provider used to convert the value. Allowed values are Base16FormatProvider, Base32FormatProvider, Base58FormatProvider, and Base64FormatProvider. |
Returns — A new byte array decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be converted, or provider is not one of the recognized Base-N format providers. |
public static bool TryGetString(ReadOnlySpan<byte> value, IFormatProvider provider, out string result)Tries to obtain a Base-N string representation of the specified value, dispatching to the codec that matches the runtime type of provider.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to convert. |
provider |
IFormatProvider |
The format provider used to obtain the base representation. |
result |
out string |
When this method returns, contains the Base-N string representation of value, or an empty string if the value cannot be converted. |
Returns — true if the conversion was successful; otherwise, false. Returns false when provider is not one of the recognized Base-N format providers.
public static bool TryGetBytes(ReadOnlySpan<char> value, IFormatProvider provider, out byte[] result)Tries to obtain a new byte array by decoding the specified Base-N value, dispatching to the codec that matches the runtime type of provider.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The value to convert. |
provider |
IFormatProvider |
The format provider used to convert the value. |
result |
out byte[] |
When this method returns, contains the byte array decoded from value, or an empty byte array if the value cannot be converted. |
Returns — true if the conversion was successful; otherwise, false. Returns false when provider is not one of the recognized Base-N format providers.
| Method | Returns | Description |
|---|---|---|
Encode(ReadOnlySpan<byte>, IFormatProvider?) |
string |
Encodes the specified value into a Base-N string. |
Decode(ReadOnlySpan<char>, IFormatProvider?) |
byte[] |
Decodes the specified Base-N value into a byte array. |
TryEncode(ReadOnlySpan<byte>, IFormatProvider?, out string) |
bool |
Tries to encode the specified value into a Base-N string. |
TryDecode(ReadOnlySpan<char>, IFormatProvider?, out byte[]) |
bool |
Tries to decode the specified Base-N value into a byte array. |
string Encode(ReadOnlySpan<byte> value, IFormatProvider? provider = null);Encodes the specified value into a Base-N string representation.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-N string representation. |
provider |
IFormatProvider? |
The format provider that will be used to encode the value. When null, the codec's default provider is used. |
Returns — A new Base-N string representation encoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be encoded. |
byte[] Decode(ReadOnlySpan<char> value, IFormatProvider? provider = null);Decodes the specified Base-N representation into a byte array.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-N value to decode into a byte array. |
provider |
IFormatProvider? |
The format provider that will be used to decode the value. When null, the codec's default provider is used. |
Returns — A new byte array decoded from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value cannot be decoded. |
bool TryEncode(ReadOnlySpan<byte> value, IFormatProvider? provider, out string result);Tries to encode the specified value into a Base-N string representation.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value to encode into a Base-N string representation. |
provider |
IFormatProvider? |
The format provider that will be used to encode the value. |
result |
out string |
When this method returns, contains the Base-N 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.
bool TryDecode(ReadOnlySpan<char> value, IFormatProvider? provider, out byte[] result);Tries to decode the specified Base-N representation into a byte array.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-N value to decode into a byte array. |
provider |
IFormatProvider? |
The format provider that will be used to decode the value. |
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.
using System;
using System.Text;
using OnixLabs.Core.Text;
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
// Base-16 (hexadecimal), uppercase.
Base16Codec base16 = new();
string hex = base16.Encode(data, Base16FormatProvider.Uppercase);
byte[] fromHex = base16.Decode(hex, Base16FormatProvider.Uppercase);
// Base-58, Bitcoin alphabet (the default when provider is null).
Base58Codec base58 = new();
string b58 = base58.Encode(data);
byte[] fromB58 = base58.Decode(b58);
Console.WriteLine(hex); // 48656C6C6F2C20576F726C6421
Console.WriteLine(Encoding.UTF8.GetString(fromB58)); // Hello, World!using OnixLabs.Core.Text;
byte[] data = [0xDE, 0xAD, 0xBE, 0xEF];
string b32 = IBaseCodec.Base32.Encode(data, Base32FormatProvider.Rfc4648);
string b64 = IBaseCodec.Base64.Encode(data);using System;
using OnixLabs.Core.Text;
byte[] data = [0xCA, 0xFE, 0xBA, 0xBE];
// The codec is selected from the runtime type of the provider.
string encoded = IBaseCodec.GetString(data, Base64FormatProvider.Rfc4648);
if (IBaseCodec.TryGetBytes(encoded, Base64FormatProvider.Rfc4648, out byte[] decoded))
{
Console.WriteLine(decoded.Length); // 4
}using System;
using OnixLabs.Core.Text;
Base16Codec codec = new();
// Lowercase input is rejected under the uppercase provider.
if (!codec.TryDecode("deadbeef", Base16FormatProvider.Uppercase, out byte[] _))
{
Console.WriteLine("Casing did not match the uppercase format provider.");
}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