可以关联的数字的基数是否有限制?

发布于 2024-09-28 10:29:03 字数 958 浏览 6 评论 0 原文

这可能是一个非常复杂和扭曲的想法。在学习一些 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-10-05 10:29:03

在这里查看我的问题。 基数 36 之后使用哪些符号

see my question here. What symbols are used after base 36

柠檬心 2024-10-05 10:29:03

是的,当然可以做到,只是不能使用 JavaScript 的 toString 函数。

您只需要决定使用哪些字符来表示您的数字。尽管有一些既定标准,但这在很大程度上是任意的。例如,请参阅 Base64Ascii85

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.

埋葬我深情 2024-10-05 10:29:03

当然可以做到,数字基数是完全任意的(在9之后使用英文字母只是一种常见的约定)。 Number#toString 专门设计用于处理基数 2 到 36:

可选的基数应该是 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:

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

From the spec, section 15.7.4.2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文