The documentation is currently being reviewed, so you may see some inconsistencies between sections.
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Encoding

Purpose

It can be useful to convert bytes to strings. For example, to represent hashes or to create shareable X25519 and Ed25519 public keys. Hex and Base64 encoding can be used to do this.

Usage

GetToHexBufferSize

Returns the output buffer size required for ToHex().

Encodings.GetToHexBufferSize(ReadOnlySpan<byte> data)

Exceptions

ArgumentOutOfRangeException

data has a length of 0.

OverflowException

data.Length * 2 has resulted in an overflow.

ToHex

Fills a span with the hexadecimal string that represents the provided data.

Encodings.ToHex(Span<char> hex, ReadOnlySpan<byte> data)

Exceptions

ArgumentOutOfRangeException

hex has a length not equal to GetToHexBufferSize().

ArgumentOutOfRangeException

data has a length of 0.

OverflowException

data.Length * 2 has resulted in an overflow.

CryptographicException

Error converting bytes to hex.

GetFromHexBufferSize

Returns the output buffer size required for FromHex().

Exceptions

ArgumentOutOfRangeException

hex has a length of 0 or is not a multiple of 2 (after discounting ignored chars).

ArgumentException

hex cannot contain non-ASCII characters.

ArgumentException

ignoreChars cannot contain hex characters or represent the only characters in hex.

FromHex

Fills a span with the data from decoding a hexadecimal string. Separator characters to ignore when parsing can optionally be provided.

Exceptions

ArgumentOutOfRangeException

data has a length not equal to GetFromHexBufferSize().

ArgumentOutOfRangeException

hex has a length of 0 or is not a multiple of 2 (after discounting ignored chars).

ArgumentException

hex cannot contain non-ASCII characters.

ArgumentException

ignoreChars cannot contain hex characters or represent the only characters in hex.

FormatException

Invalid hex string.

GetToBase64BufferSize

Returns the output buffer size required for ToBase64().

Exceptions

ArgumentOutOfRangeException

data has a length of 0.

ArgumentOutOfRangeException

variant must be a value within the enum.

OverflowException

The encoded length is too large for an array.

ToBase64

Fills a span with the Base64 string that represents the provided data. Choose one variant (e.g., Base64URL for file names/URLs) and only ever use that variant.

Exceptions

ArgumentOutOfRangeException

base64 has a length not equal to GetToBase64BufferSize().

ArgumentOutOfRangeException

data has a length of 0.

ArgumentOutOfRangeException

variant must be a value within the enum.

CryptographicException

Error converting bytes to Base64.

GetFromBase64BufferSize

Returns the output buffer size required for FromBase64().

Exceptions

ArgumentOutOfRangeException

base64 has a length of 0 or is not a valid length based on variant.

ArgumentOutOfRangeException

variant must be a value within the enum.

ArgumentException

base64 cannot contain non-ASCII characters.

ArgumentException

ignoreChars cannot contain Base64 characters or represent the only characters in base64.

OverflowException

The buffer size computation has resulted in an overflow.

FromBase64

Fills a span with the data from decoding a Base64 string. The variant must match the one used for encoding. Separator characters to ignore when parsing can optionally be provided.

Exceptions

ArgumentOutOfRangeException

data has a length not equal to GetFromBase64BufferSize().

ArgumentOutOfRangeException

base64 has a length of 0 or is not a valid length based on variant.

ArgumentOutOfRangeException

variant must be a value within the enum.

ArgumentException

base64 cannot contain non-ASCII characters.

ArgumentException

ignoreChars cannot contain Base64 characters or represent the only characters in base64.

OverflowException

The buffer size computation has resulted in an overflow.

FormatException

Invalid Base64 string.

Constants

Notes

Base64 has a better compression rate than hex. However, they tend to be used for different purposes. For instance, hex is often used for test vectors and encoding hashes, whereas Base64 is more commonly used for encoding keys.

Last updated