Crate base16 [] [src]

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
encode_upper, encode_lower Returns a new String Always
encode_config Returns a new String Always
encode_config_buf Appends to provided String If buffer needs to grow
encode_config_slice Writes to provided &[u8] Never

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
decode Returns a new Vec<u8> Always
decode_buf Appends to provided Vec<u8> If buffer needs to grow
decode_slice Writes to provided &[u8] Never

Example

extern crate base16;

fn main() {
    let original_msg = "Taako the wizard";
    let hex_string = base16::encode_lower(original_msg);
    assert_eq!(hex_string, "5461616b6f207468652077697a617264");
    let decoded = base16::decode(&hex_string).unwrap();
    assert_eq!(String::from_utf8(decoded).unwrap(), original_msg);
}

Re-exports

pub use EncConfig::*;

Enums

DecodeError

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 contains input.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 through f).

encode_upper

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