Function bs58::decode

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

Setup decoder for the given string using the default alphabet.

Examples

Basic example

assert_eq!(
    vec![0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58],
    bs58::decode("he11owor1d").into_vec()?);

Changing the alphabet

assert_eq!(
    vec![0x60, 0x65, 0xe7, 0x9b, 0xba, 0x2f, 0x78],
    bs58::decode("he11owor1d")
        .with_alphabet(bs58::Alphabet::RIPPLE)
        .into_vec()?);

Decoding into an existing buffer

let mut output = [0xFF; 10];
assert_eq!(8, bs58::decode("he11owor1d").into(&mut output)?);
assert_eq!(
    [0x04, 0x30, 0x5e, 0x2b, 0x24, 0x73, 0xf0, 0x58, 0xFF, 0xFF],
    output);

Errors

Invalid Character

assert_eq!(
    bs58::decode::Error::InvalidCharacter { character: 'l', index: 2 },
    bs58::decode("hello world").into_vec().unwrap_err());

Non-ASCII Character

assert_eq!(
    bs58::decode::Error::NonAsciiCharacter { index: 5 },
    bs58::decode("he11o🇳🇿").into_vec().unwrap_err());

Too Small Buffer

This error can only occur when reading into a provided buffer, when using into_vec() a vector large enough is guaranteed to be used.

let mut output = [0; 7];
assert_eq!(
    bs58::decode::Error::BufferTooSmall,
    bs58::decode("he11owor1d").into(&mut output).unwrap_err());