Crate base16

source · []
Expand description

This is a base16 (e.g. hexadecimal) encoding and decoding library with an emphasis on performance. The API is very similar and inspired by the base64 crate’s API, however it’s less complex (base16 is much more simple than base64).

Encoding

The config options at the moment are limited to the output case (upper vs lower).

FunctionOutputAllocatesRequires alloc feature
encode_upper, encode_lowerReturns a new StringAlwaysYes
encode_configReturns a new StringAlwaysYes
encode_config_bufAppends to provided StringIf buffer needs to growYes
encode_config_sliceWrites to provided &[u8]NeverNo

Decoding

Note that there are no config options (In the future one might be added to restrict the input character set, but it’s not clear to me that this is useful).

FunctionOutputAllocatesRequires alloc feature
decodeReturns a new Vec<u8>AlwaysYes
decode_sliceWrites to provided &[u8]NeverNo
decode_bufAppends to provided Vec<u8>If buffer needs to growYes

Features

This crate has two features, both are enabled by default and exist to allow users in no_std environments to disable various portions of .

  • The "alloc" feature, which is on by default, adds a number of helpful functions that require use of the alloc crate, but not the rest of std.

    • This is no_std compatible.
    • Each function should list whether or not it requires this feature under the Availability of its documentation.
  • The "std" feature, which is on by default, enables the "alloc" feature, and additionally makes DecodeError implement the std::error::Error trait.

    • Frustratingly, this trait is in std (and not in core or alloc), but not implementing it would be quite annoying for some users, so it’s kept, even though it’s what prevents us from being no_std compatible in all configurations.

Re-exports

pub use EncConfig::*;

Enums

Represents a problem with the data we want to decode.

Configuration options for encoding. Just specifies whether or not output should be uppercase or lowercase.

Functions

Decode bytes from base16, and return a new Vec<u8> containing the results.

Decode bytes from base16, and appends into the provided buffer. Only allocates if the buffer could not fit the data. Returns the number of bytes written.

Decode a single character as hex.

Decode bytes from base16, and write into the provided buffer. Never allocates.

Encode a single character as hex, returning a tuple containing the two encoded bytes in big-endian order – the order the characters would be in when written out (e.g. the top nibble is the first item in the tuple)

Convenience wrapper for base16::encode_byte(byte, base16::EncodeLower)

Convenience wrapper for base16::encode_byte(byte, base16::EncodeUpper)

Encode input into a string using the listed config. The resulting string contains input.len() * 2 bytes.

Encode input into the end of the provided buffer. Returns the number of bytes that were written.

Write bytes as base16 into the provided output buffer. Never allocates.

Encode bytes as base16, using lower case characters for nibbles between 10 and 15 (a through f).

Encode bytes as base16, using upper case characters for nibbles between 10 and 15 (A through F).