Repository navigation
OnixLabs.Core.Text.Base64
Represents a Base-64 encoded value: an immutable wrapper around a sequence of bytes whose textual representation is produced and consumed using a Base-64 alphabet.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → ValueType → Base64
|
| Implements |
IBaseValue<Base64> (and through it IValueEquatable<Base64>, ISpanParsable<Base64>, IBinaryConvertible, ISpanFormattable) |
public readonly partial struct Base64 : IBaseValue<Base64>Base64 models a binary value together with its Base-64 textual form. Base-64 is a binary-to-text encoding that represents binary data using a 64-character alphabet, encoding three bytes of input as four characters of output and padding the final group with the = character as required. It is one of the most common encodings for transmitting binary data over text-oriented protocols.
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-64 codec exposed through OnixLabs.Core.Text.Codecs; the alphabet used during encoding and decoding is selected by an IFormatProvider, namely a Base64FormatProvider (the supplied variant is Rfc4648).
Equality is based on value semantics: two Base64 instances are equal when their underlying byte sequences are equal. The default (parameterless) ToString() encodes the value using the default Base-64 alphabet.
using OnixLabs.Core.Text;
byte[] data = [1, 2, 3, 4];
Base64 value = new(data);
// Implicit conversion from a byte span is also supported.
Base64 converted = data.AsSpan();using System.Text;
using OnixLabs.Core.Text;
// The characters are first encoded to bytes using the specified (or default) encoding,
// then wrapped as a Base64 value.
Base64 value = new("Hello, World!", Encoding.UTF8);using OnixLabs.Core.Text;
Base64 value = new(new byte[] { 0, 1, 2, 3 });
// Encode using the default alphabet.
string encoded = value.ToString();
// Encode using an explicit alphabet.
string rfc4648 = value.ToString(Base64FormatProvider.Rfc4648);
// Round-trip back to a Base64 value.
Base64 parsed = Base64.Parse(encoded);
if (Base64.TryParse(encoded, null, out Base64 result))
{
// result is equal to value.
}Base64 value = new(new byte[] { 10, 20, 30 });
ReadOnlySpan<byte> span = value.AsReadOnlySpan();
ReadOnlyMemory<byte> memory = value.AsReadOnlyMemory();| Constructor | Description |
|---|---|
Base64(ReadOnlySpan<byte>) |
Initializes a new instance from a read-only span of bytes. |
Base64(ReadOnlyMemory<byte>) |
Initializes a new instance from a read-only memory region of bytes. |
Base64(ReadOnlySequence<byte>) |
Initializes a new instance from a read-only sequence of bytes. |
Base64(ReadOnlySpan<char>, Encoding?) |
Initializes a new instance from a read-only span of characters using the specified encoding. |
Base64(ReadOnlyMemory<char>, Encoding?) |
Initializes a new instance from a read-only memory region of characters using the specified encoding. |
Base64(ReadOnlySequence<char>, Encoding?) |
Initializes a new instance from a read-only sequence of characters using the specified encoding. |
public Base64(ReadOnlySpan<byte> value)Initializes a new instance of the Base64 struct. A defensive copy of the supplied bytes is taken.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<byte> |
The value from which to initialize the new Base64 instance. |
public Base64(ReadOnlyMemory<byte> value)Initializes a new instance of the Base64 struct. A defensive copy of the supplied bytes is taken.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlyMemory<byte> |
The value from which to initialize the new Base64 instance. |
public Base64(ReadOnlySequence<byte> value)Initializes a new instance of the Base64 struct. A defensive copy of the supplied bytes is taken.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySequence<byte> |
The value from which to initialize the new Base64 instance. |
public Base64(ReadOnlySpan<char> value, Encoding? encoding = null)Initializes a new instance of the Base64 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 Base64 instance. |
encoding |
Encoding? |
The Encoding used to transform the specified value, or null to use the default encoding. |
public Base64(ReadOnlyMemory<char> value, Encoding? encoding = null)Initializes a new instance of the Base64 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 Base64 instance. |
encoding |
Encoding? |
The Encoding used to transform the specified value, or null to use the default encoding. |
public Base64(ReadOnlySequence<char> value, Encoding? encoding = null)Initializes a new instance of the Base64 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 Base64 instance. |
encoding |
Encoding? |
The Encoding used to transform the specified value, or null to use the default encoding. |
| Method | Returns | Description |
|---|---|---|
Parse(string, IFormatProvider?) |
Base64 |
Decodes the specified Base-64 string into a Base64 value. |
Parse(ReadOnlySpan<char>, IFormatProvider?) |
Base64 |
Decodes the specified Base-64 character span into a Base64 value. |
TryParse(string?, IFormatProvider?, out Base64) |
bool |
Attempts to decode the specified Base-64 string into a Base64 value. |
TryParse(ReadOnlySpan<char>, IFormatProvider?, out Base64) |
bool |
Attempts to decode the specified Base-64 character span into a Base64 value. |
| 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(Base64) |
bool |
Determines whether this value equals another Base64 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 default Base-64 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. |
public static Base64 Parse(string value, IFormatProvider? provider = null)Decodes the specified Base-64 string into a Base64 value.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The Base-64 encoded string to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-64 alphabet, or null to use the default. |
Returns — A new Base64 value decoded from value.
public static Base64 Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)Decodes the specified Base-64 character span into a Base64 value.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-64 encoded characters to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-64 alphabet, or null to use the default. |
Returns — A new Base64 value decoded from value.
public static bool TryParse(string? value, IFormatProvider? provider, out Base64 result)Attempts to decode the specified Base-64 string into a Base64 value.
| Parameter | Type | Description |
|---|---|---|
value |
string? |
The Base-64 encoded string to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-64 alphabet, or null to use the default. |
result |
out Base64 |
When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value. |
Returns — true if value was decoded successfully; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out Base64 result)Attempts to decode the specified Base-64 character span into a Base64 value.
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The Base-64 encoded characters to decode. |
provider |
IFormatProvider? |
The format provider that supplies the Base-64 alphabet, or null to use the default. |
result |
out Base64 |
When this method returns, contains the decoded value if decoding succeeded; otherwise, the default value. |
Returns — true if value was decoded successfully; otherwise, false.
public ReadOnlySpan<byte> AsReadOnlySpan()Gets a read-only span over the underlying bytes of the current Base64 value.
Returns — A ReadOnlySpan<byte> over the underlying value.
public ReadOnlyMemory<byte> AsReadOnlyMemory()Gets a read-only memory region over the underlying bytes of the current Base64 value.
Returns — A ReadOnlyMemory<byte> over the underlying value.
public bool Equals(Base64 other)Determines whether the current Base64 value is equal to another Base64 value by comparing their underlying byte sequences.
Returns — true if the values are equal; otherwise, false.
public override bool Equals(object? obj)Determines whether the current Base64 value is equal to another object.
Returns — true if obj is a Base64 with an equal underlying value; otherwise, false.
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.
public override string ToString()Encodes the current value into its Base-64 string representation using the default Base-64 alphabet.
Returns — The Base-64 encoded string representation of the value.
public string ToString(IFormatProvider? formatProvider)Encodes the current value into its Base-64 string representation using the alphabet supplied by the specified format provider.
| Parameter | Type | Description |
|---|---|---|
formatProvider |
IFormatProvider? |
The format provider that supplies the Base-64 alphabet, or null to use the default. |
Returns — The Base-64 encoded string representation of the value.
public string ToString(string? format, IFormatProvider? formatProvider)Encodes the current value into its Base-64 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-64 alphabet, or null to use the default. |
Returns — The Base-64 encoded string representation of the value.
public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvider)Encodes the current value into its Base-64 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-64 alphabet, or null to use the default. |
Returns — The Base-64 encoded string representation of the value.
| Operator | Description |
|---|---|
operator == |
Determines whether two Base64 values are equal. |
operator != |
Determines whether two Base64 values are not equal. |
implicit operator Base64(ReadOnlySpan<byte>) |
Converts a read-only span of bytes to a Base64 value. |
implicit operator Base64(ReadOnlyMemory<byte>) |
Converts a read-only memory region of bytes to a Base64 value. |
implicit operator Base64(ReadOnlySequence<byte>) |
Converts a read-only sequence of bytes to a Base64 value. |
implicit operator Base64(ReadOnlySpan<char>) |
Converts a read-only span of characters to a Base64 value using the default encoding. |
implicit operator Base64(ReadOnlyMemory<char>) |
Converts a read-only memory region of characters to a Base64 value using the default encoding. |
implicit operator Base64(ReadOnlySequence<char>) |
Converts a read-only sequence of characters to a Base64 value using the default encoding. |
public static bool operator ==(Base64 left, Base64 right)Performs an equality comparison between two Base64 values. Returns true if left is equal to right; otherwise, false.
public static bool operator !=(Base64 left, Base64 right)Performs an inequality comparison between two Base64 values. Returns true if left is not equal to right; otherwise, false.
public static implicit operator Base64(ReadOnlySpan<byte> value)
public static implicit operator Base64(ReadOnlyMemory<byte> value)
public static implicit operator Base64(ReadOnlySequence<byte> value)
public static implicit operator Base64(ReadOnlySpan<char> value)
public static implicit operator Base64(ReadOnlyMemory<char> value)
public static implicit operator Base64(ReadOnlySequence<char> value)Implicitly converts the specified value into a new Base64 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.
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