ChatGPT解决这个技术问题 Extra ChatGPT

在 JavaScript 中将字符转换为 ASCII 码

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

例如:

从 "\n" 中得到 10。

请注意,大多数答案中建议的 String.prototype.charCodeAt() 方法将返回 UTF-16 代码单元(由于历史原因,甚至不是完全正确的 UTF-16 编码)。只有前 128 个 Unicode 代码点与 ASCII 字符编码直接匹配。
@ÁlvaroGonzález 这是一个重要的警告。如何获取 ASCII 码,例如 128 代表
@simlev ASCII 没有任何 符号,它是在货币出现之前几十年创建的。 128 是它在 Windows-1252 中的编码。在随机编码之间进行转换本身就是一个问题。

J
Jim
"\n".charCodeAt(0);

与此相反的是String.fromCharCode(10)
有趣的事实:您实际上并不需要 0(第一个参数值)——只需 "\n".charCodeAt() 就可以了。
@MathiasBynens:幸运的是,这是记录在案的:developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…。 “如果不是数字,则默认为0”
您应该指出,与 String.fromCharCode( asciiNumVal ) 不同,stringInstance.charCodeAt( index ) 不是 String 类的静态方法
@Mathias Bynens,它当然默认为零,但我只是出于兴趣对性能进行了测试,它的性能**与使用 0 相比相对较差。jsperf.com/default-to-0-vs-0/4 ** 它只是一个相对差异,无论哪种方式都非常快.
s
schu34

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": ""
}

更好的 ascii 参考:en.wikipedia.org/wiki/ASCII - 我很自豪我为该页面上的表格所做的着色在将近 10 年后仍然存在 :)
@theGrayFox C:\> man ascii 提供 Bad command or file name
请注意,这些方法与 UTF-16 兼容,这意味着根据输入字符串,charCodeAt 可以扩展到远远超过 1 字节 ASCII 值 0-127。如果 JavaScript 接受和处理任意字符串输入,请不要假设它在该范围内。
@e2-e4 亲爱的开发人员,您使用了错误的操作系统。 man 是一个linux命令。不确定windows等价物。使用浏览器上的搜索引擎在 Windows 上搜索“man ascii”可能更安全,一个结果-> man7.org/linux/man-pages/man7/ascii.7.html
M
Marco Altieri

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

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

省略 0...

它曾经比 'n'.charCodeAt(0) 慢得多,但我现在已经对其进行了测试,我看不出有任何区别(在有和没有 0 的情况下执行了 100 亿次)。仅在 Chrome 和 Firefox 中进行性能测试。


这实际上需要更长的时间。只使用零会更快。 (在我的电脑上,经过几万次迭代,它花费了大约两倍的时间——0.055 秒与 0.126 秒)
F
Francisco Presencia

虽然其他答案是正确的,但我更喜欢这种方式:

function ascii (a) { return a.charCodeAt(0); }

然后,要使用它,只需:

var lineBreak = ascii("\n");

我将其用于小型快捷方式系统:

$(window).keypress(function(event) {
  if (event.ctrlKey && event.which == ascii("s")) {
    savecontent();
    }
  // ...
  });

你甚至可以在 map() 或其他方法中使用它:

var ints = 'ergtrer'.split('').map(ascii);

只是为了美观,一种新的 ES6 编写方式:const ascii = a => a.charCodeAt(0);
C
Community

对于那些想要获取字符串的所有 ASCII 代码总和的人:

'Foobar'
  .split('')
  .map(x=>x.charCodeAt(0))
  .reduce((a,b)=>a+b);

或者,ES6:

[...'Foobar']
  .map(char => char.charCodeAt(0))
  .reduce((current, previous) => previous + current)

仔细检查你的最后一行。
优雅的!作为函数: function ascii(str) { return str .split('') .map(function (char) { return char + ": " + String(char.charCodeAt(0)) + "\n"; }) .reduce(function (current, previous) { return current + previous; }); }
[...'Foobar'].reduce((i,s)=>s.charCodeAt(0)+i,0)
I
Ibrahim Lawal

为确保完整的 Unicode 支持和可逆性,请考虑使用:

'\n'.codePointAt(0);

这将确保在测试超过 UTF-16 限制的字符时,您将获得它们的真实代码点值。

例如

'𐩕'.codePointAt(0); // 68181
String.fromCodePoint(68181); // '𐩕'

'𐩕'.charCodeAt(0);  // 55298
String.fromCharCode(55298);  // '�'

将特殊字符转换为十六进制表示法也可能很有用,因为某些文本编辑器可能无法正常工作,必须直接处理此类字符。例如:警报(str.hexEncode().hexDecode());
D
Daniel

JavaScript 将字符串存储为 UTF-16(双字节),因此如果您想忽略第二个字节,只需在 0000000011111111(即 255)上使用按位 & 运算符将其删除:

'a'.charCodeAt(0) & 255 === 97; // because 'a' = 97 0 
'b'.charCodeAt(0) & 255 === 98; // because 'b' = 98 0 
'✓'.charCodeAt(0) & 255 === 19; // because '✓' = 19 39

为什么要忽略第二个字节?
问题是关于从 UTF-16 字符串(双字节)生成 ASCII。如果您未能忽略第二个字节,迟早您会得到非 ASCII 码。
@Steven de Salas - 您为非 ASCII 字符获取非 ASCII 代码的“解决方案”是返回错误的 ASCII 代码??
@CarlSmith,不是错误的。只是为了去掉字符的非 ascii 组件。如果您使用单字节,这很有用。不过,您的项目可能需要不同的解决方案。
K
KyleMit

将字符串转换为 UTF-8 的数组(流):

const str_to_arr_of_UTF8 = new TextEncoder().encode("Adfgdfs");
// [65, 100, 102, 103, 100, 102, 115]

注意:ASCII 是 UTF-8 的子集,所以这是一个通用的解决方案


R
Roko C. Buljan

要将字符串转换为累积数字:

const stringToSum = str => [...str||"A"].reduce((a, x) => a += x.codePointAt(0), 0); console.log(stringToSum("A")); // 65 console.log(stringToSum("Roko")); // 411 console.log(stringToSum("堆栈溢出")); // 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 `

$ {user.name[0].toUpperCase()}
`; }; [ {name:"A"}, {name:"Amanda"}, {name:"amanda"}, {name:"Anna"}, ].forEach(user => { document.body.insertAdjacentHTML("beforeend" , UI_userIcon(user)); }); .UserIcon { 宽度:4em;高度:4em;边界半径:4em;显示:inline-flex;证明内容:中心;对齐项目:居中; } .UserIcon-letter { 字体:700 2em/0 sans-serif;颜色:#fff; }


为什么是数字“65”?那代表什么?
-65 是完全可选的。由于 ASCII“A”字符数字 = 65。 - 65 是可选的,用于获取第一个(“A”)字符作为 int 0
m
maioman

为了支持 ES6 中的所有 UTF-16(还有 non-BMP/supplementary characters),可以使用 string.codePointAt() 方法;

此方法是 charCodeAt 的改进版本,它仅支持 <65536 的 unicode 代码点(216 - 单个 16 位)。


值得一提的是,String.prototype.codePointAt()not supported by any versions of Internet Explorer。 (但它在 Edge 上受支持。)您可以获得一个 polyfill here
1
1.21 gigawatts

您可以输入一个字符并使用此代码获取 Ascii 代码

例如输入一个像 A 这样的字符,你会得到 Ascii 代码 65

函数 myFunction(){ var str=document.getElementById("id1"); if (str.value=="") { str.focus();返回; } var a="ASCII 码是 == > "; document.getElementById("demo").innerHTML =a+str.value.charCodeAt(0); }

检查ASCII码

输入任意字符:

< button onclick="myFunction()">获取ASCII码


a
ata
str.charCodeAt(index)

使用 charCodeAt() 以下示例返回 65,即 A 的 Unicode 值。

'ABC'.charCodeAt(0) // 返回 65


s
simlev

如果您仅使用 128 个原始 ASCII 字符(代码 0 到 127),则扩展 Álvaro González 和其他人的评论,charCodeAtcodePointAt 非常好。在此范围之外,代码取决于字符集,如果您希望结果有意义,则需要在计算之前进行字符集转换。

让我们以欧元符号为例:'€'.codePointAt(0) 返回 8364,它远远超出 0-127 范围,并且与 UTF-16(或 UTF-8)字符集相关。

我正在移植一个 Visual Basic 程序,并注意到它使用 Asc 函数来获取字符代码。显然,从它的角度来看,它会返回 Windows-1252 字符集中的字符代码。为了确保获得相同的数字,我需要转换字符串字符集,然后计算代码。

非常简单,例如在 Python 中:ord('€'.encode('Windows-1252'))
然而,为了在 Javascript 中实现同样的效果,我不得不求助于缓冲区和 conversion library

iconv = require('iconv-lite');
buf = iconv.encode("€", 'win1252');
buf.forEach(console.log);

t
tejas_spy007

charCodeAt(0);

上面的代码在大多数情况下都有效,但是在处理单词以根据上面的代码查找排名时有一个问题。例如,aa 的排名为 97+97 = 194(实际为 1+1 = 2),而 w 的排名为 119(实际为 23),这使得 aa > w。为了解决这个从上面的结果减去 96,从 1 开始定位。

charCodeAt(0) - 96;


alphabetically firstlater 呢?我建议不要使用字符代码的总和进行排名,而是直接比较每个字符。
m
menomanabdulla

对于那些想要获得具有平均值的字符串的所有 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!'))


c
claypooj

正如其他人指出的那样,ASCII 仅涵盖 128 个字符(包括非打印字符)。为了向后兼容,Unicode 将 ASCII 作为其前 128 个字符,但它还包含更多字符。

要仅获取 ASCII 字符代码作为整数,您可以执行以下操作:

function ascii_code (character) {
  
  // Get the decimal code
  let code = character.charCodeAt(0);

  // If the code is 0-127 (which are the ASCII codes,
  if (code < 128) {
    
    // Return the code obtained.
    return code;

  // If the code is 128 or greater (which are expanded Unicode characters),
  }else{

    // Return -1 so the user knows this isn't an ASCII character.
    return -1;
  };
};

如果您只查找字符串中的 ASCII 字符(例如,对字符串进行 slugifying),您可以执行以下操作:

function ascii_out (str) {
  // Takes a string and removes non-ASCII characters.

  // For each character in the string,
  for (let i=0; i < str.length; i++) {

    // If the character is outside the first 128 characters (which are the ASCII
    // characters),
    if (str.charCodeAt(i) > 127) {

      // Remove this character and all others like it.
      str = str.replace(new RegExp(str[i],"g"),'');

      // Decrement the index, since you just removed the character you were on.
      i--;
    };
  };
  return str
};

来源

https://www.geeksforgeeks.org/ascii-vs-unicode/#:~:text=Unicode%20is%20the%20universal%20character,encoding%20standard%20for%20electronic%20communication。

https://www.w3schools.com/jsref/jsref_charcodeat.asp