Function bs58::encode

source · []
pub fn encode<I: AsRef<[u8]>>(input: I) -> EncodeBuilder<'static, I>
Expand description

Setup encoder for the given bytes using the default alphabet.

Examples

Basic example

let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
assert_eq!("he11owor1d", bs58::encode(input).into_string());

Changing the alphabet

let input = [0x60, 0x65, 0xe7, 0x9b, 0xba, 0x2f, 0x78];
assert_eq!(
    "he11owor1d",
    bs58::encode(input)
        .with_alphabet(bs58::Alphabet::RIPPLE)
        .into_string());

Encoding into an existing string

let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = "goodbye world".to_owned();
bs58::encode(input).into(&mut output)?;
assert_eq!("he11owor1d", output);

Errors

Too Small Buffer

This error can only occur when reading into an unresizeable buffer.

let input = [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58];
let mut output = [0; 7];
assert_eq!(
    bs58::encode::Error::BufferTooSmall,
    bs58::encode(input).into(&mut output[..]).unwrap_err());