2

var sVar = "someString";

I tried sVar.toString(36) as inferred from num.toString(2). But it doesn't work.

I know to convert from base64 you use atob/btoa but I could not find for base 36. Maybe there is a variant for this that does it?

4
  • string to base36... sVar has string – Muhammad Umer Feb 14 '15 at 4:21
  • What is the value of sVar here? – jdphenix Feb 14 '15 at 4:22
  • 1
    I want to convert "string" to Base36 – Muhammad Umer Feb 14 '15 at 4:22
  • 4
    This question isn't a duplicate. The linked question is how to convert a base 10 number to base 36. This question is on how to convert a string to a base 36 number. Two different questions that have two different answers. – imjosh Jun 21 '16 at 20:42
3

You can use parseInt to convert a string to a base-36 integer.

var myString = "somestring";
var myNum = parseInt(myString, 36); /* 2913141654103084 */

And you can use .toString to convert back to a string.

myNum.toString(36) /* "somestring" */

Both functions take a numeric "radix" (an integer between 2 and 36 specifying the base to use for representing numeric values) as a parameter, which should be 36 for base-36.

4
  • reddit.com/dev/api#fullnames according to this i should have gotten 15bfi0 for string aww .. can you explain it.. – Muhammad Umer Feb 14 '15 at 4:49
  • but i get this 14144 – Muhammad Umer Feb 14 '15 at 4:50
  • 3
    that code only works on some strings. e.g. decode36(encode36("FooBarW000t")) -> foobarw000w – imjosh Jun 21 '16 at 20:42
  • ok the problem was that reddit converts integer id into base36 NOT the name of the subreddit. – Muhammad Umer Dec 19 '18 at 21:47

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