这可能是一个非常复杂和扭曲的想法。在学习一些 JavaScript 时得到的。这激起了我的心。希望可能有人之前已经考虑过这个问题或者可以启发我:-)
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"
这让我对基础进行了更多的探索。我记得我的数字工程课程并了解到,对于大于 10 的基数从 10 开始的每个数字,将从“a”、“b”开始命名。
例如:
var n = 10;
var y = 11;
string = n.toString(12); // Evaluates to 'a'
string = y.toString(12); // Evaluates to 'b'
然后我明白这可能会上升到“z”
如此,
var n = 35;
string = n.toString(36); // Evaluates to "z"
但这似乎就结束了。如果我们
var n = 35;
string = n.toString(37); // Gives error Error: illegal radix 37 { message="illegal radix 37", more...}
这样做,那么我相信我们只能数到以 36 为底的数。因为对于以 37 为底的计数系统,我们将无法数到 36,因为我们用完了英文字符。我们有办法做到吗?也许我们可以添加其他角色。
我知道这是非常无用的东西,我们在生活中永远不需要它。
但有人已经考虑过了吗?
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?
发布评论
评论(3)
在这里查看我的问题。 基数 36 之后使用哪些符号
see my question here. What symbols are used after base 36
是的,当然可以做到,只是不能使用 JavaScript 的
toString
函数。您只需要决定使用哪些字符来表示您的数字。尽管有一些既定标准,但这在很大程度上是任意的。例如,请参阅 Base64、Ascii85 等
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.
你当然可以做到,数字基数是完全任意的(在
9
之后使用英文字母只是一种常见的约定)。Number#toString
专门设计用于处理基数 2 到 36:来自 规范,第 15.7.4.2 节。
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:From the spec, section 15.7.4.2.