What Id like to do is convert a string from one "alphabet" to another, like converting numbers between bases, but more abstract and with arbitrary digits.
For example, converting β255β from the alphabet β0123456789β to the alphabet β0123456789ABCDEFβ will result in βFFβ. One way to do this is to convert the input string to an integer, and then back. For example: (pseudo code)
int decode(string input, string alphabet) { int value = 0; for(i = 0; i < input.length; i++) { int index = alphabet.indexOf(input[i]); value += index * pow(alphabet.length, input.length - i - 1); } return value; } string encode(int value, string alphabet) { string encoded = ""; while(value > 0) { int index = value % alphabet.length; encoded = alphabet[index] + encoded; value = floor(value / alphabet.length); } return encoded; }
So decode("255", "0123456789") returns the integer 255, and encode(255, "0123456789ABCDEF") returns "FF" .
This works for small alphabets, but Id like to use base 26 (all uppercase letters) or base 52 (upper and lower case) or base 62 (upper case, lowercase and numbers) and values ββthat potentially exceed one hundred digits. The algorithm described above will theoretically work for such alphabets, but in practice Im works in integer overflows because the numbers get so big so fast when you start doing 62 ^ 100.
What is interesting to me if there is an algorithm for such a conversion without the need to support such giant integers? Perhaps a way to start outputting the result before processing the entire input string?
My intuition tells me that this is possible, but my math skills are missing. Any help would be appreciated.
There are several similar questions in StackOverflow, but none of them are what I'm looking for.
source share