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

ToHex

Returns the hexadecimal string that represents the data.

Encodings.ToHex(ReadOnlySpan<byte> data)

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

data has a length of 0.

FormatExceptionarrow-up-right

The data could not be converted to hex. This should never be thrown.

FromHex

Returns a byte array from a hexadecimal string. Common separator characters are ignored by default. ignoreChars can be null to disallow any non-hexadecimal characters.

Encodings.FromHex(string hex, string ignoreChars = ":- ")

Exceptions

ArgumentNullExceptionarrow-up-right

hex is null.

ArgumentOutOfRangeExceptionarrow-up-right

hex has a length of 0.

FormatExceptionarrow-up-right

Unable to parse the hex string.

ToBase64

Returns the Base64 string representing the data. Choose one variant (e.g. Base64URL for file names/URLs) and only ever use that variant.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

data has a length of 0.

FormatExceptionarrow-up-right

The data could not be converted to Base64. This should never be thrown.

FromBase64

Returns a byte array from a Base64 string. The variant must match the one used for encoding. ignoreChars can be null to disallow any non-Base64 characters.

Exceptions

ArgumentNullExceptionarrow-up-right

base64 is null.

ArgumentOutOfRangeExceptionarrow-up-right

base64 has a length of 0.

FormatExceptionarrow-up-right

Unable to parse the Base64 string.

Constants

Notes

triangle-exclamation
circle-check
circle-info

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 vectorsarrow-up-right and encoding hashesarrow-up-right, whereas Base64 is more commonly used for encoding keysarrow-up-right.

Last updated