Home > Web Front-end > HTML Tutorial > How to convert Unicode values ​​to characters in JavaScript?

How to convert Unicode values ​​to characters in JavaScript?

王林
Release: 2023-08-28 10:17:02
forward
1325 people have browsed it

How to convert Unicode values ​​to characters in JavaScript?

In this tutorial, we will learn to convert Unicode values ​​to characters in JavaScript. The Unicode value is the standard value of a character and can be encoded by the user to convert it into a character.

For example, "A" is a Unicode character whose value is 65 according to the ASCII (American Standard Code for Information Interchange) table. Likewise, all letters, numbers, and other characters have specific Unicode values. We will learn to use JavaScript to identify Unicode characters from their values.

Use fromCharCode()method

In JavaScript, the string library contains the fromCharCode() method, which gets the decimal value of a Unicode character and returns the associated character. Additionally, it takes multiple values ​​as arguments and returns a string after combining all characters.

grammar

We can use the fromCharCode() method to convert Unicode values ​​to characters according to the following syntax.

String.fromCharCode( value );
String.fromCharCode( v1, v2, v3, v4, v5, … );
Copy after login

parameter

  • value - It is the decimal value of the Unicode character.

  • v1, v2, v3, ... - They are multiple values ​​of different or the same Unicode value.

Example 1

In the example below, we use the fromCharCode() method on different Unicode values ​​to convert them to characters. We convert two Unicode values ​​- 69 and 97 into characters.

<html>
<head>
</head>
<body>
   <h2>Convert Unicode values to characters in JavaScript.</h2>
   <h4>Converting single Decimal Unicode value to character using the <i> fromCharCode() </i> method.</h4>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
   
      // converting different decimal values to characters
      let value = 69;
      let char = String.fromCharCode( value );
      output.innerHTML += value + " to unicode character is : " + char + " <br/> ";
      char = String.fromCharCode( 97 );
      output.innerHTML += 97 + " to unicode character is : " + char + " <br/> ";
   </script>
</body>
</html>
Copy after login

Example 2

In the following program, we use the fromCharCode() method to convert multiple Unicode values ​​to characters. We convert two sets of Unicode values ​​to char strings, the first to "TutorialsPoint" and the second to "Hello".

<html>
<head>
</head>
<body>
   <h2>Convert Unicode values to characters in JavaScript. </h2>
   <h4>Converting multiple Decimal Unicode values to characters using the <i> fromCharCode() </i> method.</h4>
   <p id="output1"></p>
   <script>
      let output1 = document.getElementById("output1");
   
      // converting multiple values to unicode characters in single pass.
      output1.innerHTML += " [84, 117, 116, 111, 114, 105, 97, 108, 115, 80, 111, 105, 110, 116] to Unicode character is : " + String.fromCharCode( 84, 117, 116, 111, 114, 105, 97, 108, 115, 80, 111, 105, 110, 116 ) + " <br/> ";
      output1.innerHTML += " [72, 69, 76, 76, 79] to Unicode character is : " + String.fromCharCode( 72, 69, 76, 76, 79 ) + " <br/> ";
   </script>
</body>
</html>
Copy after login

In the above output, users can see that when we pass multiple values ​​of Unicode characters to the fromCharCode() method, it will output a string of related Unicode characters.

Users have learned and understood how the fromCharCode() method converts Unicode values ​​to Unicode characters. Users can use the fromCharCode() method whether it is a single value or multiple values ​​separated by commas.

The above is the detailed content of How to convert Unicode values ​​to characters in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template