pub const fn encode<const OUT: usize>(
input: &[u8],
config: Config,
) -> Result<ArrayStr<OUT>, WrongOutputLength>Expand description
Encodes input into an ArrayStr<OUT>
with the encoding determined by config.
§Errors
This function returns a WrongOutputLength error when
OUT doesn’t equal encoded_len(input.len(), config).
§Example
§Base 64
use const_base::{ArrayStr, Config, WrongOutputLength, encode};
{
const ENCODED: &ArrayStr<16> =
&WrongOutputLength::unwrap(encode(b"hello worl", Config::B64));
assert_eq!(ENCODED, "aGVsbG8gd29ybA==");
}
{
const CFG: Config = Config::B64.end_padding(false);
const ENCODED: &ArrayStr<4> = &WrongOutputLength::unwrap(encode(b"BYE", CFG));
assert_eq!(ENCODED, "QllF");
}§Base 32
use const_base::{ArrayStr, Config, WrongOutputLength, encode};
{
const ENCODED: &ArrayStr<8> = &WrongOutputLength::unwrap(encode(b"fox", Config::B32));
assert_eq!(ENCODED, "MZXXQ===");
}
{
const CFG: Config = Config::B32.end_padding(false);
const ENCODED: &ArrayStr<5> = &WrongOutputLength::unwrap(encode(b"dog", CFG));
assert_eq!(ENCODED, "MRXWO");
}§Hexadecimal
use const_base::{ArrayStr, Config, WrongOutputLength, encode};
{
const LOWER: &ArrayStr<8> = &WrongOutputLength::unwrap(encode(b"bluh", Config::HEX_LOWER));
const UPPER: &ArrayStr<8> = &WrongOutputLength::unwrap(encode(b"bluh", Config::HEX));
assert_eq!(LOWER, "626c7568");
assert_eq!(UPPER, "626C7568");
}