如何使用JavaScript将字符转换为ASCII码?

例如:

从“\n”中得到10。


当前回答

将字符串转换为累积数:

const stringToSum = str => [...str||”A“].reduce((a, x) => and += x.codePointAt(0), 0); console.log(stringToSum(“A”)); 65 console.log(stringToSum(“Roko”)); 411 console.log(stringToSum(“Stack Overflow”));1386

用例:

假设你想根据用户名生成不同的背景颜色:

const stringToSum = str => [...str||"A"].reduce((a, x) => a += x.codePointAt(0), 0); const UI_userIcon = user => { const hue = (stringToSum(user.name) - 65) % 360; // "A" = hue: 0 console.log(`Hue: ${hue}`); return `<div class="UserIcon" style="background:hsl(${hue}, 80%, 60%)" title="${user.name}"> <span class="UserIcon-letter">${user.name[0].toUpperCase()}</span> </div>`; }; [ {name:"A"}, {name:"Amanda"}, {name:"amanda"}, {name:"Anna"}, ].forEach(user => { document.body.insertAdjacentHTML("beforeend", UI_userIcon(user)); }); .UserIcon { width: 4em; height: 4em; border-radius: 4em; display: inline-flex; justify-content: center; align-items: center; } .UserIcon-letter { font: 700 2em/0 sans-serif; color: #fff; }

其他回答

"\n".charCodeAt(0);

string .prototype. charcodeat()可以将字符串字符转换为ASCII数字。例如:

"ABC".charCodeAt(0) // returns 65

相反,使用String.fromCharCode(10)将数字转换为相等的ASCII字符。此函数可以接受多个数字,并连接所有字符,然后返回字符串。例子:

String.fromCharCode(65,66,67); // returns 'ABC'

这里是一个快速的ASCII字符参考:

{
"31": "",      "32": " ",     "33": "!",     "34": "\"",    "35": "#",    
"36": "$",     "37": "%",     "38": "&",     "39": "'",     "40": "(",    
"41": ")",     "42": "*",     "43": "+",     "44": ",",     "45": "-",    
"46": ".",     "47": "/",     "48": "0",     "49": "1",     "50": "2",    
"51": "3",     "52": "4",     "53": "5",     "54": "6",     "55": "7",    
"56": "8",     "57": "9",     "58": ":",     "59": ";",     "60": "<",    
"61": "=",     "62": ">",     "63": "?",     "64": "@",     "65": "A",    
"66": "B",     "67": "C",     "68": "D",     "69": "E",     "70": "F",    
"71": "G",     "72": "H",     "73": "I",     "74": "J",     "75": "K",    
"76": "L",     "77": "M",     "78": "N",     "79": "O",     "80": "P",    
"81": "Q",     "82": "R",     "83": "S",     "84": "T",     "85": "U",    
"86": "V",     "87": "W",     "88": "X",     "89": "Y",     "90": "Z",    
"91": "[",     "92": "\\",    "93": "]",     "94": "^",     "95": "_",    
"96": "`",     "97": "a",     "98": "b",     "99": "c",     "100": "d",    
"101": "e",    "102": "f",    "103": "g",    "104": "h",    "105": "i",    
"106": "j",    "107": "k",    "108": "l",    "109": "m",    "110": "n",    
"111": "o",    "112": "p",    "113": "q",    "114": "r",    "115": "s",    
"116": "t",    "117": "u",    "118": "v",    "119": "w",    "120": "x",    
"121": "y",    "122": "z",    "123": "{",    "124": "|",    "125": "}",    
"126": "~",    "127": ""
}

如果你只有一个字符而不是字符串,你可以使用:

'\n'.charCodeAt();
'\n'.codePointAt();

省略0…

它曾经比'n'. charcodeat(0)慢得多,但我现在已经测试过了,我再也看不到任何区别了(带0和不带0执行了100亿次)。仅在Chrome和Firefox中测试了性能。

对于那些想要获取字符串中所有ASCII码的平均值的人:

const ASCIIAverage = (str) =>Math.floor(str.split("))。map(item => item. charcodeat (0)).reduce((prev,next) => prev+next)/str.length) console.log (ASCIIAverage(“Hello World !”)

将字符串转换为累积数:

const stringToSum = str => [...str||”A“].reduce((a, x) => and += x.codePointAt(0), 0); console.log(stringToSum(“A”)); 65 console.log(stringToSum(“Roko”)); 411 console.log(stringToSum(“Stack Overflow”));1386

用例:

假设你想根据用户名生成不同的背景颜色:

const stringToSum = str => [...str||"A"].reduce((a, x) => a += x.codePointAt(0), 0); const UI_userIcon = user => { const hue = (stringToSum(user.name) - 65) % 360; // "A" = hue: 0 console.log(`Hue: ${hue}`); return `<div class="UserIcon" style="background:hsl(${hue}, 80%, 60%)" title="${user.name}"> <span class="UserIcon-letter">${user.name[0].toUpperCase()}</span> </div>`; }; [ {name:"A"}, {name:"Amanda"}, {name:"amanda"}, {name:"Anna"}, ].forEach(user => { document.body.insertAdjacentHTML("beforeend", UI_userIcon(user)); }); .UserIcon { width: 4em; height: 4em; border-radius: 4em; display: inline-flex; justify-content: center; align-items: center; } .UserIcon-letter { font: 700 2em/0 sans-serif; color: #fff; }