Function bs62::decode_num[][src]

pub fn decode_num(inp: &str) -> Result<BigUint, Box<dyn Error>>

Decode a base62 string to a base10 (decimal) number.

Algorithm

The algorithm starts by assigning a 0 to a number variable. Then each char of the input string is converted to its according index in the alphabet. The number is then multiplied by 62 (the base) and the index of the char is added to the number. This is repeated until all chars have been consumed.

The returned num_bigint::BigUint can be converted to a primitive type using the num_traits::ToPrimitive trait.

Errors

An error variant is returned when the input string contains invlid chars.

Example

use num_traits::ToPrimitive;
let big_uint = bs62::decode_num("A")?;
let num = big_uint.to_i32().ok_or("Unable to convert `BigUint` to `i32`")?;

assert_eq!(num, 10_i32);