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

htmlasciijavascript

ASCII 代表美国信息交换标准代码,这是一种通过将字符分配给特定数值来编码字符的方法。这样分配的数值称为 ASCII 代码,在计算机系统中广泛用于表示各种字符,包括字母、数字、标点符号和特殊控制字符。

示例 1:使用 charCodeAt()

以下代码说明了一个程序,该程序使用"charCodeAt()"函数从用户接收输入字符,然后显示该字符的相应 ASCII 代码。

语法

string.charCodeAt(index)

算法

步骤 1:首先声明 HTML 标签。

步骤 2:使用内部 CSS 向网页添加一些 UI 功能。

步骤 3:创建一个输入字段以允许用户输入字符。

步骤 4:创建一个按钮,该按钮将触发将字符转换为 ASCII 的函数。

步骤 5:创建一个脚本标签。

步骤 6:在 <script> 中声明一个转换函数标签。

步骤 7:声明输入变量以接受用户的输入。

步骤 8:使用 charCodeAt() 函数将字符转换为 ASCII 值。

示例

<!DOCTYPE html>
<html>
<head>
  <title>Character to ASCII Converter</title>
//CSS 样式从这里开始
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 20px;
      text-align: center;
    }

    h1 {
      color: #333;
    }

    .container {
      max-width: 400px;
      margin: 0 auto;
      background-color: #fff;
      padding: 20px;
      border-radius: 5px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    input[type="text"] {
      width: 100%;
      padding: 10px;
      margin-bottom: 10px;
      border: 1px solid #ccc;
      border-radius: 3px;
      box-sizing: border-box;
      font-size: 16px;
    }

    button {
      width: 100%;
      padding: 10px;
      background-color: #4caf50;
      color: #fff;
      border: none;
      border-radius: 3px;
      cursor: pointer;
      font-size: 16px;
    }

    p {
      margin-top: 10px;
      font-size: 18px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Character to ASCII Converter</h1>
    //Designing input area for user
    <input type="text" id="inputText" placeholder="Enter a character">
    //creating button that convert the value
    <button onclick="convert()">Convert</button>
    <p id="output"></p>
  </div>

  <script>
  //Javascript code for converting characters to ASCII code 
  //creating function that will invoke as the user click on the button
    function convert() {
      const input = document.getElementById("inputText").value;
      const asciiCode = input.charCodeAt(0);
      document.getElementById("output").textContent = `ASCII code: ${asciiCode}`;
    }
  </script>
</body>
</html>

此内置函数以字符串和索引为参数,并返回该索引处字符的 ASCII 代码。用户在 HTML 示例中的输入字段中输入一个字符。单击"转换"按钮时,JavaScript 代码将检索该字符,应用 charCodeAt() 函数,并在网页上显示 ASCII 代码。

示例 2 - 使用 codePointAt()

codePointAt() 函数是 JavaScript 中用于字符串的固有方法。它准确地获取确定索引处字符的 Unicode 代码点值。

在此特定代码中,它用于从用户输入中检索初始字符的 Unicode 代码点值并将其显示为输出。

语法

const codePoint = input.codePointAt(0);

Example

<!DOCTYPE html>
<html>
<head>
  <title>Character to ASCII Converter Using charPointAt()</title>
  //CSS 样式从这里开始
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 20px;
      text-align: center;
    }

    h1 {
      color: #333;
    }

    .container {
      max-width: 400px;
      margin: 0 auto;
      background-color: #fff;
      padding: 20px;
      border-radius: 5px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    input[type="text"] {
      width: 100%;
      padding: 10px;
      margin-bottom: 10px;
      border: 1px solid #ccc;
      border-radius: 3px;
      box-sizing: border-box;
      font-size: 16px;
    }

    button {
      width: 100%;
      padding: 10px;
      background-color: #4caf50;
      color: #fff;
      border: none;
      border-radius: 3px;
      cursor: pointer;
      font-size: 16px;
    }

    p {
      margin-top: 10px;
      font-size: 18px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Character to ASCII Converter Using charPointAt()</h1>
     //Designing input area for user
    <input type="text" id="inputText" placeholder="Enter a character">
    <button onclick="convert()">Convert</button>
    <p id="output"></p>
  </div>
   //Javascript code for converting characters to ASCII code
  <script>
     //creating function that will invoke as the user click on the button
    function convert() {
      const input = document.getElementById("inputText").value;
      const codePoint = input.codePointAt(0);
      document.getElementById("output").textContent = `Unicode code point: ${codePoint}`;
    }
  </script>
</body>
</html>

提供的代码利用名为"codePointAt()"的 JavaScript 函数将字符转换为其对应的 Unicode 代码点。它包含一个输入字段,您可以在其中键入字符。单击"转换"按钮后,代码将计算并显示输入字符的 Unicode 代码点。输出显示在 id 为"output"的段落元素中。此功能可让您方便地获取字符的 Unicode 代码点值。

结论

在 JavaScript 中将字符转换为 ASCII 码是一项基本操作,在各种编程场景中都有实际应用。通过使用 charCodeAt() 和 charPointAt() 等方法,开发人员可以轻松获取字符的 ASCII 码或 Unicode 代码点值。无论您需要专门使用 ASCII 码还是处理更广泛的字符,JavaScript 都提供了这些通用方法来帮助您高效准确地执行字符到 ASCII 的转换。


相关文章