Class BaseNCodec
Codec that enables easy conversion from an array of bytes to any numeric base in [2, 256) and back again, using a supplied custom alphabet.
Implemented by treating the input byte sequence to encode verbatim as a big-endian
BigInteger and iteratively doing a divmod operation until
the quotient is zero, emitting the modulus mapped onto the alphabet for each iteration.
Decoding reverses this process, ending up with the same BigInteger as in
the initial encoding step.
Note that BigIntegers represent the canonical form of any given
integer, which means that leading zero bytes are implicitly ignored. We therefore
special-case this by unary-coding the number of leading zeroes in the encoded form,
where a leading zero byte is mapped to the first character of the alphabet.
Example for Base58, which starts its alphabet with 1 (0 is not present):
"Hello World!" = "2NEpo7TZRRrLZSi2U" "\0\0Hello World!" = "112NEpo7TZRRrLZSi2U" (note leading 1s)
Example for Base62, which starts its alphabet with 0:
"Hello World!" = "T8dgcjRGkZ3aysdN" "\0\0Hello World!" = "00T8dgcjRGkZ3aysdN" (node leading 0s)
Important: runtime complexity is O(n2) for both
encoding and decoding, so this should only be used to encode/decode relatively short
byte sequences. This is not a replacement for Base64 etc. encoding that runs
in linear time! In addition, a BaseNCodec with a Base64 alphabet encodes
to a completely different output than a regular Base64 encoder when the input is not
evenly divisible by three. This is due to regular Base64 explicitly handling padding,
while this codec does not.
- Author:
- vekterli
-
Field Summary
Fields -
Method Summary
-
Field Details
-
MAX_BASE
public static final int MAX_BASE- See Also:
-
-
Method Details
-
of
-
base
public int base() -
encode
-
decode
-