Function bs62::encode_num [−][src]
pub fn encode_num<T: ToBigUint>(num: &T) -> String
Expand description
Convert a base10 (decimal) number to base62.
Algorithm
The input number is divided (with reminder) by 62 (the base). The remainder
is then used as an index to get a char from the alphabet [0-9][A-Z][a-z]
and the char is stored an a list. The floored quotiont is then used as the
new number and is again divided (with remainder) by 62 and so on. At the end,
when the number becomes zero, and the las remainer was used to get char from
the alphabet, the list of chars is reversed and converted to a string.
Panics
Panics if the num
can not be converted to BigUint
(e.g. if num
is negative).
Example
let num = 1337; let encoded = bs62::encode_num(&num); assert_eq!(encoded, "LZ")