A Base62 encoder / decoder with support for leading zero bytes.
Normally, during the conversion of base10 (decimal) to base62, the input data is interpreted as one large number:
[0x13, 0x37] => 0x1337 => 4919
As leading zeroes do not count to the value of a number (0001337 = 1337
),
they are ignored while converting the number to base62.
encode_num
] function does.encode_data
] keeps these leading zeroes.This is achieved by prepending a 0x01
byte to the data before encoding,
thus creating a number that starts with a 1
, then maybe has some zeroes and
finally some other digits (0001337 => 10001337 => (No zeroes are removed)
).
The leading 0x01
is removed after the data has been decoded from bse62 back
to base10: 10001337 => 0001337
txt
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
This is the same alphabet that
CyberChef
uses by default: [0-9][A-Z][a-z]
.
Wikipedia/Base62 suggests another
alphabet ([A-Z][a-z][0-9]
) but I found that starting with numbers is more
natural as base62 is actually a number system like decimal (which is actually
base10).
This method will prepend 0x01
to the data before encoding it.
```rust let data = vec![0x13, 0x37]; let encoded = bs62::encode_data(&data);
assert_eq!(encoded, "IKN") ```
This method expects a leading 0x01
in the byte array after decoding. It
removes the first byte before returning the byte array.
```rust # # let encoded = "IKN"; let data = bs62::decode_data(&encoded)?;
asserteq!(data, vec![0x13u8, 0x37]); # ```
```rust let num = 1337; let encoded = bs62::encode_num(&num);
assert_eq!(encoded, "LZ") ```
```rust let num = 1337; let encoded = bs62::encode_num(&num);
assert_eq!(encoded, "LZ") ```
bs62
is licensed under MIT.
See the LICENSE.txt file in this repository for more information.