2
$\begingroup$

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"]
$\endgroup$
3
  • 1
    $\begingroup$ 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. $\endgroup$
    – lericr
    Commented Aug 26, 2022 at 23:15
  • 1
    $\begingroup$ 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. $\endgroup$
    – lericr
    Commented Aug 26, 2022 at 23:16
  • 2
    $\begingroup$ 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). $\endgroup$
    – Roman
    Commented Aug 27, 2022 at 8:48

2 Answers 2

5
$\begingroup$

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]
$\endgroup$
0
$\begingroup$

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" *)
$\endgroup$
1
  • $\begingroup$ -1: This does not answer the question as written ("What is the cleanest way to convert "OjUpRAA" back into the number 1000000000000?") $\endgroup$ Commented Aug 27, 2022 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.