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).
Function | Output | Allocates | Requires alloc feature |
---|---|---|---|
encode_upper , encode_lower | Returns a new String | Always | Yes |
encode_config | Returns a new String | Always | Yes |
encode_config_buf | Appends to provided String | If buffer needs to grow | Yes |
encode_config_slice | Writes to provided &[u8] | Never | No |
§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).
Function | Output | Allocates | Requires alloc feature |
---|---|---|---|
decode | Returns a new Vec<u8> | Always | Yes |
decode_slice | Writes to provided &[u8] | Never | No |
decode_buf | Appends to provided Vec<u8> | If buffer needs to grow | Yes |
§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 thealloc
crate, but not the rest ofstd
.- This is
no_std
compatible. - Each function should list whether or not it requires this feature
under the
Availability
of its documentation.
- This is
-
The
"std"
feature, which is on by default, enables the"alloc"
feature, and additionally makesDecodeError
implement thestd::error::Error
trait.- Frustratingly, this trait is in
std
(and not incore
oralloc
), but not implementing it would be quite annoying for some users, so it’s kept, even though it’s what prevents us from beingno_std
compatible in all configurations.
- Frustratingly, this trait is in
Re-exports§
pub use EncConfig::*;
Enums§
- Decode
Error - Represents a problem with the data we want to decode.
- EncConfig
- Configuration options for encoding. Just specifies whether or not output should be uppercase or lowercase.
Functions§
- decode
- Decode bytes from base16, and return a new
Vec<u8>
containing the results. - decode_
buf - 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_
byte - Decode a single character as hex.
- decode_
slice - Decode bytes from base16, and write into the provided buffer. Never allocates.
- encode_
byte - 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)
- encode_
byte_ l - Convenience wrapper for
base16::encode_byte(byte, base16::EncodeLower)
- encode_
byte_ u - Convenience wrapper for
base16::encode_byte(byte, base16::EncodeUpper)
- encode_
config - Encode
input
into a string using the listed config. The resulting string containsinput.len() * 2
bytes. - encode_
config_ buf - Encode
input
into the end of the provided buffer. Returns the number of bytes that were written. - encode_
config_ slice - Write bytes as base16 into the provided output buffer. Never allocates.
- encode_
lower - Encode bytes as base16, using lower case characters for nibbles between 10
and 15 (
a
throughf
). - encode_
upper - Encode bytes as base16, using upper case characters for nibbles between
10 and 15 (
A
throughF
).