# How can I convert the output of IntegerString[n, "Base64"] back into base 10?

BaseForm[] does not allow conversion to bases above base 36. However, IntegerString[] can convert a number n into a base 64 string.

n = 1000000000000;
s = IntegerString[n, "Base64"]
(* "OjUpRAA" *)


What is the cleanest way to convert "OjUpRAA" back into the number 1000000000000?

None of the expressions below do the job:

ImportString[s, "Base64"]
ImportString[s, {"Base64", "String"}]
FromDigits[s, 64]
FromDigits[s, "Base64"]
FromDigits[Characters[s], 64]
BaseDecode[s, "Base64"]

• Base64 does not mean using a set of 64 digits. Base64 is a binary encoding scheme. It's a way to turn a stream of bits (which could represent any type of data, e.g. string or numeric or anything else) into a stream of textual characters. Obviously such a scheme will use 64 characters, and you could interpret these as digits, but I don't think Mathematica will assume that a Base64 encoded snippet of text will represent a base-64 representation of a number. Commented Aug 26, 2022 at 23:15
• You could supply the reverse mapping yourself. You'd split the string up into characters, replace each character with it's digit (each digit represented in base-10) and then use FromDigits on that sequence. Commented Aug 26, 2022 at 23:16
• Slightly off-topic: any Mathematica object can be converted to/from Base64 with the encoder enc = BaseEncode@*BinarySerialize and the decoder dec = BinaryDeserialize@*BaseDecode. This encoding preserves the internal structure of the object (not just its value). For example, enc[1000000000000] gives "ODpMABCl1OgAAAA=" instead of the OP's "OjUpRAA". Anything can be encoded: enc[Exp[x]] gives "ODpmAnMFUG93ZXJzAUVzCEdsb2JhbGB4", and enc@MandelbrotSetPlot[] gives something huge (but decodable). Commented Aug 27, 2022 at 8:48

Here's something you could try. First, build the mapping to "decode" a Base64 encoding as if it represented a digit sequence:

Base64ToDigitMap = Table[IntegerString[i, "Base64"] -> i, {i, 0, 63}]


Now, split up your Base64-encoded string into characters, apply this map to it, and use FromDigits:

FromDigits[Characters["OjUpRAA"] /. Base64ToDigitMap, 64]


The inverse of IntegerString is FromDigits. However FromDigits does not support "Base64". Therefore, do not use IntegerString, use ExportString instead, which has an inverse ImportString that works with Base64.

n = 1000000000000;
ex = ExportString[ToString@n, "Base64"]
(* MTAwMDAwMDAwMDAwMA== *)


To decode this:

ImportString[s, "Base64"]
(* "1000000000000" *)

• -1: This does not answer the question as written ("What is the cleanest way to convert "OjUpRAA" back into the number 1000000000000?") Commented Aug 27, 2022 at 11:02