简体   繁体   English

如何使用javascript将数字转换为字符?

[英]How to convert number to character using javascript?

How to convert 1234567890 = ABCDEFGHIJ , For eg.如何转换1234567890 = ABCDEFGHIJ ,例如。 360 to CFJ

I know how to do it for single character:我知道如何为单个字符做到这一点:

var chr = String.fromCharCode(97 + n); // where n is 0, 1, 2 ...

but not sure how can I do it for multiple/group of number at once: For eg.但不确定如何一次为多个/一组数字执行此操作:例如。 230 to BCJ

This would work:这会起作用:

 function convert(num) { return num .toString() // convert number to string .split('') // convert string to array of characters .map(Number) // parse characters as numbers .map(n => (n || 10) + 64) // convert to char code, correcting for J .map(c => String.fromCharCode(c)) // convert char codes to strings .join(''); // join values together } console.log(convert(360)); console.log(convert(230));

And just for fun, here's a version using Ramda :只是为了好玩,这里有一个使用Ramda的版本:

 const digitStrToChar = R.pipe( Number, // convert digit to number R.or(R.__, 10), // correct for J R.add(64), // add 64 R.unary(String.fromCharCode) // convert char code to letter ); const convert = R.pipe( R.toString, // convert number to string R.split(''), // split digits into array R.map(digitStrToChar), // convert digit strings to letters R.join('') // combine letters ); console.log(convert(360)); console.log(convert(230));
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

The fromCharCode accepts a list argument as parameter. fromCharCode接受一个列表参数作为参数。

String.fromCharCode(72, 69, 76, 76, 79); for example will print 'HELLO'.例如将打印“你好”。

Your example data is invalid though.不过,您的示例数据无效。 The letter 'A' for example is 65. You'll need to create a comma separated argument that you feed into the function.例如,字母“A”是 65。您需要创建一个逗号分隔的参数,并将其输入到函数中。 If you don't provide it as a comma separated arg, you'll be trying to parse a single key code which will most likely fail.如果您不将其作为逗号分隔的 arg 提供,您将尝试解析很可能会失败的单个键代码。

 console.log( (1234567890 + '').replace(/\\d/g, c => 'JABCDEFGHI'[c] ) ) console.log( String.fromCharCode(...[...1234567890 + ''].map(c => (+c || 10) | 64)) )

You'll need to define your alphabet up front.您需要预先定义您的字母表。 For your snippet, you seem to want 1-9 to map to AI, and 0 to J, so:对于您的代码段,您似乎希望将 1-9 映射到 AI,将 0 映射到 J,因此:

const alphabet = "JABCDEFGHI";

Then it's just a matter of using each digit of the number as an index in that alphabet:然后只需将数字的每个数字用作该字母表中的索引即可:

function numberToString(num) {
    // Split out each digit of the number:
    const digits = Math.floor(num).toString().split("").map(Number);

    // Then create a new string using the alphabet:
    return digits.reduce((str, digit) => {
        return str + alphabet[digit];
    }, "");
}

That should satisfy your constraints.那应该满足你的限制。 Here's an example of this in action:这是一个实际的例子:

 const alphabet = "JABCDEFGHI"; function numberToString(num) { // Split out each digit of the number: const digits = Math.floor(num).toString().split("").map(Number); // Then create a new string using the alphabet: return digits.reduce((str, digit) => { return str + alphabet[digit]; }, ""); } console.log(numberToString(360)) // CFJ console.log(numberToString(230)) // BCJ

For uppercase, 97 was the wrong offset, and it didn't give you the desired result with 0 being J .对于大写, 97是错误的偏移量,它没有给你想要的结果, 0J

360 to CFJ 360转CFJ

230 to BCJ 230 为 BCJ

 function convert(n) { return String.fromCharCode( ...n.toString().split("").map(c => (+c||10) + 0x40) ); } console.log(convert(360)); console.log(convert(230)); function onInput(elm){ console.clear() console.log( convert(elm.value) ) }
 <input oninput="onInput(this)">


And you can do this without the initial string conversion.您可以在没有初始字符串转换的情况下执行此操作。

 function convert(n) { var a = Array(Math.floor(Math.log10(n))+1); var i = a.length; while (n) { a[--i] = ((n%10)||10) + 0x40; n = Math.floor(n/10); } return String.fromCharCode(...a); } console.log(convert(360)); console.log(convert(230));

Option 1: map it选项 1:映射它

 const from = 1234567890; const answer = from .toString() // convert to string, ignore this if it's already a string .split('') // split to array .map(x => +x + 97) // convert to char code number .map(String.fromCharCode) // convert to char code .join(''); // convert back to string console.log(answer)

Option 2: pass in multiple params选项 2:传入多个参数

 const from = 1234567890; const fromCharCodes = from .toString() // convert to string, ignore this if it's already a string .split('') // split to array .map(x => +x + 97) // convert to char code number const answer = String.fromCharCode(...fromCharCodes); console.log(answer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript限制括号中的字符数 - How to limit the character number in bracket using javascript 如何将一个表情符号字符转换为 JavaScript 中的 Unicode 代码点编号? - How to convert one emoji character to Unicode codepoint number in JavaScript? 如何使用Javascript将十进制数转换为百分比 - How to convert a decimal number into percentage using Javascript 如何使用JavaScript将唯一数字转换为字符串 - how to convert a unique number in to string using javascript 如何使用JavaScript将Unicode转换为显示在网页中的字符? - How to convert Unicode to character which is displayed in web page using JavaScript? 如何使用 JavaScript 将浮点数转换为固定浮点数 - How to convert a floating number to fixed floating number using JavaScript 如何使用javascript计算textarea中键入字符的数量? - How to count number of typed character in textarea using javascript? 如何使用javascript分隔方程式中的数字和特殊字符? - How to separate the number and special character in equation using javascript? 如何在JavaScript中使用正则表达式在数字和字符/字符串之间插入空格 - How to insert space between a number and a character /string using regex in javascript 使用javascript将数字转换为日期 - Convert number into date using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM