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

例如:

从“\n”中得到10。


当前回答

正如其他人指出的那样,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字符(例如,slugified一个字符串),你可以这样做:

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/: ~:文本Unicode % 20 = % 20 % 20普遍% 20字符,编码% 20标准% 20 % 20电子% 20的沟通。 https://www.w3schools.com/jsref/jsref_charcodeat.asp

其他回答

为了支持来自ES6的所有UTF-16(也非bmp /补充字符),string.codePointAt()方法可用;

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

"\n".charCodeAt(0);

虽然其他答案都是正确的,但我更喜欢这样:

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);
str.charCodeAt(index)

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

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

正如其他人指出的那样,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字符(例如,slugified一个字符串),你可以这样做:

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/: ~:文本Unicode % 20 = % 20 % 20普遍% 20字符,编码% 20标准% 20 % 20电子% 20的沟通。 https://www.w3schools.com/jsref/jsref_charcodeat.asp