Click to See Complete Forum and Search --> : [RESOLVED] ToString(36) Conversion


code?
May 5th, 2009, 10:50 PM
Alright, so if I use this code right here.
var strCode = (46328).toString(36); // strCode = "zqw"

How do I convert it back to the original integer?

olivthill2
May 6th, 2009, 07:24 AM
Using Javascript, if you have a string of characters, s, representing a number, you can convert it to a number, i, with var i = Number(s);

PeejAvery
May 6th, 2009, 07:48 AM
Using Number() would return NaN. In this case, since you parsed it with a radix, you simply reverse it with the same radix.

parseInt(strCode, 36);

EDIT: In fact, unless you plan to use it as a string, you should be using parseInt to change from various INT systems.

code?
May 16th, 2009, 05:05 PM
What about a custom ToString function, that can take any base, and a custom character map.

What's the algorithm to convert it back (assume using same base and char map)?

PeejAvery
May 17th, 2009, 09:50 AM
Well, since it would be custom made, why wouldn't you just create a reverse function of your custom toString function?

code?
May 21st, 2009, 08:32 PM
See, there's the problem, I don't know how to make the reverse function.

PeejAvery
May 21st, 2009, 10:32 PM
Just do the steps in your first function backwards.