Macro const_base::encode
source · macro_rules! encode {
($slice:expr, $config:expr $(,)*) => { ... };
}
Expand description
Encodes the $slice
constant into a &'static ArrayStr<LEN>
,
with the encoding determined by $config
.
$slice
can be a &'static str
, &'static [u8; N]
, or &'static [u8]
.
There’s also the encode_as_str
macro for getting a &'static str
.
encode_as_str
is just an alias for encode!(...).as_str()
.
Examples
Base 64
use const_base::{encode, ArrayStr, Config};
{
const OUT: &ArrayStr<4> = encode!("bar", Config::B64);
assert_eq!(OUT, "YmFy");
}
{
const BYTES: &[u8] = b"world";
// this macro can encode non-literal constants
const OUT: &[u8; 8] = encode!(BYTES, Config::B64_URL_SAFE).as_array();
const OUT_STR: &str = encode!(BYTES, Config::B64_URL_SAFE).as_str();
assert_eq!(OUT, b"d29ybGQ=");
assert_eq!(OUT_STR, "d29ybGQ=");
}
Base 32
use const_base::{encode, Config};
const OUT: &str = encode!(&[3, 5, 8], Config::B32).as_str();
assert_eq!(OUT, "AMCQQ===");
Base 32
use const_base::{encode, Config};
const OUT: &str = encode!(&[3, 5, 8], Config::B32).as_str();
assert_eq!(OUT, "AMCQQ===");
Hexadecimal
use const_base::{encode, Config};
const LOWER: &str = encode!(&[0xB0, 0x01], Config::HEX_LOWER).as_str();
const UPPER: &str = encode!(&[0xB0, 0x01], Config::HEX).as_str();
assert_eq!(LOWER, "b001");
assert_eq!(UPPER, "B001");