LoginSignup
13
7

More than 5 years have passed since last update.

ECMAScriptでは基数変換に2進数から36進数まで使える

Posted at

よく忘れるので書くことにした。10進数から36進数の計算、ふつうに実装しても良いのだけど、ほとんどのJavaScript処理系では1メソッドで書ける。

ECMAScript 5.1を読むと、次の様に書いてある。

15.7.4.2 Number.prototype.toString ( [ radix ] )
The optional radix should be an integer value in the inclusive range 2 to 36.

はい。そして現在使われているほとんどの処理系(Node.js、Chrome、MSIE、Opera、Safari)は36進数変換に対応している。逆に36進数をNumberするには、parseInt(string, radix)で良い。

n = 300;
console.log(n, n.toString(36)); // -> 300 "8c" 
console.log("8c", parseInt("8c", 36)); // -> 8c 300 
13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7