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

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

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

示例 1:使用 charCodeAt()

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

语法

string.charCodeAt(index)

算法

第 1 步:从声明 HTML 标记开始。

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

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

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

第 5 步:创建脚本标记。

第 6 步:

第 7 步:声明输入变量以获取用户的输入。

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

示例

<!DOCTYPE html>
<html>
<head>
  <title>Character to ASCII Converter</title>
//CSS Styling from here
  <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);

示例

<!DOCTYPE html>
<html>
<head>
  <title>Character to ASCII Converter Using charPointAt()</title>
  //CSS Styling from here
  <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 的转换。

以上就是如何使用 JavaScript 将字符转换为 ASCII 代码?的详细内容,更多请关注编程之家其它相关文章!

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小