将整数转换为其等效字符,其中 0 => a, 1 =>乙等
我想根据字母表将整数转换为其等效字符。例如:
0 => a
1 => b
2 => c
3 => d
等等。我可以构建一个数组,然后在需要时查找它,但我想知道是否有一个内置函数可以为我执行此操作。我通过 Google 找到的所有示例都使用 ASCII 值,而不是字符在字母表中的位置。
I want to convert an integer into its character equivalent based on the alphabet. For example:
0 => a
1 => b
2 => c
3 => d
etc. I could build an array and just look it up when I need it but I’m wondering if there’s a built in function to do this for me. All the examples I’ve found via Google are working with ASCII values and not a character’s position in the alphabet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
假设您想要小写字母:
97 是小写“a”的 ASCII 代码。如果您需要大写字母,请将 97 替换为 65(大写“A”)。请注意,如果
n > 25
,您将超出字母范围。Assuming you want lower case letters:
97 is the ASCII code for lower case 'a'. If you want uppercase letters, replace 97 with 65 (uppercase 'A'). Note that if
n > 25
, you will get out of the range of letters.如果扩展到其他字母表,将更加便携:
或者,更加兼容(与我们心爱的 IE):
Will be more portable in case of extending to other alphabets:
or, to be more compatible (with our beloved IE):
如果您不介意返回多字符字符串,则可以支持任意正索引:(
未彻底测试精度错误:)
If you don't mind getting multi-character strings back, you can support arbitrary positive indices:
(Not thoroughly tested for precision errors :)
一个简单的答案是(26 个字符):
如果空间很宝贵,您可以执行以下操作(20 个字符):
想想您可以用所有这些额外的字节做什么!
其工作原理是将数字转换为基数 36,这样就得到了以下字符:
通过偏移 10,字符从
a
开始,而不是0
。但不完全确定客户端运行两个不同示例的速度有多快。
A simple answer would be (26 characters):
If space is precious you could do the following (20 characters):
Think about what you could do with all those extra bytes!
How this works is you convert the number to base 36, so you have the following characters:
By offsetting by 10 the characters start at
a
instead of0
.Not entirely sure about how fast running the two different examples client-side would compare though.
我不喜欢所有使用
97
或36
等幻数的解决方案。假定字母为大写,“A”从 0 开始。
I don't like all the solutions that use magic numbers like
97
or36
.this assumes uppercase letters and starts 'A' at 0.
Javascript 的 String.fromCharCode(code1, code2, ..., codeN) 接受无限数量的参数并返回一个字母字符串,其对应的 ASCII 值为 code1, code2, ... codeN。由于 97 在 ASCII 中是“a”,因此我们可以通过在索引中添加 97 来调整索引。
Javascript's String.fromCharCode(code1, code2, ..., codeN) takes an infinite number of arguments and returns a string of letters whose corresponding ASCII values are code1, code2, ... codeN. Since 97 is 'a' in ASCII, we can adjust for your indexing by adding 97 to your index.
使用
String.fromCharCode
。这会从 Unicode 值返回一个字符串,该字符串与 ASCII 的前 128 个字符匹配。Use
String.fromCharCode
. This returns a string from a Unicode value, which matches the first 128 characters of ASCII.就这样:(a-zA-Z)
输入:0-51,否则会返回false(范围错误);
OR:
如果范围错误,则返回未定义。
注意:该数组只会创建一次,并且由于闭包,它将可用于新的 codeToChar 函数。我想它比第一种方法更快(基本上只是一个查找)。
There you go: (a-zA-Z)
input: 0-51, or it will return false (range error);
OR:
returns undefined in case of range error.
NOTE: the array will be created only once and because of closure it will be available for the the new codeToChar function. I guess it's even faster then the first method (it's just a lookup basically).
尝试
Try
@mikemaccana 的出色解决方案的唯一问题是它使用二进制 >>运营商的成本高昂,性能明智。我建议对他的伟大著作进行修改,作为一个小小的改进,这样你的同事也许可以更容易地阅读。
或者作为单行
示例:
The only problem with @mikemaccana's great solution is that it uses the binary >> operator which is costly, performance-wise. I suggest this modification to his great work as a slight improvement that your colleagues can perhaps read more easily.
Or as a one-liner
Example:
如果您正在寻找 TypeScript 工作函数,请按照
您可以进行多项检查,例如
(numericDigit >= 1 && numericDigit <= 26) ?
根据要求在函数体内。If you are looking for TypeScript working functions then follow
You can make several checks like
(numericDigit >= 1 && numericDigit <= 26) ?
inside function body as per the requirements.假设您想要大写字母:
示例:
numberToLetter('023') 为 ["A", "C", "D"]
numberToLetter('5 ') 是“F”
Assuming you want uppercase case letters:
Example:
numberToLetter('023') is ["A", "C", "D"]
numberToLetter('5') is "F"