Currently I am working at an algorithm to encode a normal string with each possible character to a Base36 string.
I have tried the following but it doesn't work.
public static String encode(String str) {
return new BigInteger(str, 16).toString(36);
}
I guess it's because the string is not just a hex string. If I use the string "Hello22334!" In Base36, then I get a NumberFormatException
.
My approach would be to convert each character to a number. Convert the numbers to the hexadecimal representation, and then convert the hexstring to Base36.
Is my approach okay or is there a simpler or better way?
BigInteger
with base 16 should fit together. You'll probably want to convert the string to bytes first and convert those. Just keep in mind that the byte representation of a string depends on the encoding that is used and that if you don't provide an encoding the system default will be used (and this can change when running on a different system). – Stringyjava.util.Base64
is implemented and adapt that to base 36. – Stringy