Expand description
§Base62 Converter & Deconverter
Convert your positive integers to ASCII-safe String
s for situations where a concise String
representation of an integer is desired, such as when automatically generating file names or identifiers.
Each possible integer value is assigned a unique String
representation, allowing for lossless conversion between String
s and u32
values. Each additional character in a String
representation can be thought of as another digit in a Base62 system. The Base62 system is similar to the hexadecimal system except that instead of only 16 digits, 62 digits are used. Here is a list of all 62 digits used by this crate, in order from least to greatest value:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Since the String
representation of integers is based on Base62, lower values have shorter representations, but thanks to the large amount of values each digit could be, representations (of 32-bit integers) are never more than 6 characters long.
§Example
As a quick example, let’s encode a value using the encode
function from the encoding
module, then decode it using the decode
function from the decoding
module, like so:
let number: u32 = 52 * 62 * 62 + 9 * 62 + 35; // = 200481
let representation = encoding::encode(number,Some(3)).unwrap();
assert_eq!(String::from("Q9z"),representation);
assert_eq!(number,decoding::decode(representation.chars()).unwrap());
§Alternatives
This library creates output similar to base-62, but the implementation is distinct. The main advantages of this library (as of now) are encoding 0
to the string "0"
rather than an empty String, and allowing for users to give Option
al hints about the size of encoded values (which are used to pre-allocate String capacity). base-62 has the advantage of allowing encoding and decoding of unsigned integers in &[u8]
format rather than only u32 format.
In general, it is best to use base-62 when large numbers or byte sequences need to be encoded, and to use this library in most other situations.