views:

43

answers:

3

This may be very convoluted and twisted idea. Got it while learning some JavaScript. It piqued my mind. Hoping there may be someone else who would have thought about it before or might enlighten me :-)

var n = 17;

binary_string = n.toString(2);        // Evaluates to "10001"
octal_string = "0" + n.toString(8);   // Evaluates to "021"
hex_string = "0x" + n.toString(16);   // Evaluates to "0x11"

This made me probe more into bases. I remembered my Digital Engineering course and understood that for every number from 10 for a base greater than 10 will be started naming from 'a', 'b' onwards.

for eg:

var n = 10;
var y = 11;
string = n.toString(12); // Evaluates to 'a'
string = y.toString(12); // Evaluates to 'b'

Then I understood this could go uptil 'z' Thus

var n = 35;
string = n.toString(36); // Evaluates to "z"

But that seems to be the end. if we do

var n = 35;
string = n.toString(37); // Gives error Error: illegal radix 37 { message="illegal radix 37", more...}

Thus I believe we can only count up to the bases of 36. since for a base 37 counting system, we wont be able to count 36 since we exhausted English characters. Is there anyway we can do it? Maybe we can add other characters.

I know it is very useless thing, and never in life will we ever need it.

But have anyone thought about it already?

+1  A: 

Yes, of course it can be done, just not with JavaScript's toString function.

You just need to decide on which characters to use for your digits. This is largely arbitrary, although there are some established standards. See, for example, Base64, Ascii85 etc.

LukeH
Sorry, You r answer was completely correct. But since more discussion is goin on in Preet Sangha's qtn. I'm accepting it as the accepted ans. However +1
Christy John
+1  A: 

You can certainly do it, number bases are complete arbitrary (using the English alphabet after 9 is just a common convention). Number#toString is specifically designed to handle base 2 through 36:

The optional radix should be an integer value in the inclusive range 2 to 36.

From the spec, section 15.7.4.2.

T.J. Crowder