Home >Web Front-end >Front-end Q&A >How to convert characters and ascii codes to each other in javascript
In programming, we often need to convert characters to ASCII codes or convert ASCII codes to characters. In JavaScript, these operations can be easily implemented.
1. Convert characters to ASCII codes
In JavaScript, you can convert a character to the corresponding ASCII code through the charCodeAt() method.
The following is a simple example:
var char = "A"; var asciiCode = char.charCodeAt(0); console.log(asciiCode); //输出65
In this example, we define a character variable "char" and use the charCodeAt() method to convert it to the corresponding ASCII code. Since the character "A" has index 0, we pass 0 to the charCodeAt() method.
2. Convert ASCII codes to characters
The String.fromCharCode() method in JavaScript can convert ASCII codes to corresponding characters.
The following is a simple example:
var asciiCode = 65; var char = String.fromCharCode(asciiCode); console.log(char); //输出A
In this example, we define an ASCII code variable "asciiCode" and use the String.fromCharCode() method to convert it to the corresponding character.
3. Convert a string into an ASCII code array
We can also convert a string into an ASCII code array. This can be achieved by splitting the string into an array of characters and calling the charCodeAt() method for each character.
The following is a simple example:
var str = "Hello, World!"; var asciiArray = []; for (var i = 0; i < str.length; i++) { asciiArray.push(str.charCodeAt(i)); } console.log(asciiArray); //输出[72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
In this example, we define a string variable "str" and split it into a character array. We then use a for loop to iterate through each character and push its ASCII code into the "asciiArray" array.
4. Convert ASCII code array to string
We can also convert ASCII code array to corresponding string. This can be done by converting each ASCII code to a character using the String.fromCharCode() method and concatenating them using the join() method.
The following is a simple example:
var asciiArray = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]; var str = ""; for (var i = 0; i < asciiArray.length; i++) { str += String.fromCharCode(asciiArray[i]); } console.log(str); //输出Hello, World!
In this example, we define an ASCII code array variable "asciiArray" and use a for loop to traverse each ASCII code. We then use the String.fromCharCode() method to convert the ASCII code to the corresponding character and concatenate them using the "=" operator.
Summary:
In JavaScript, you can use the charCodeAt() method to convert characters to ASCII codes, and use the String.fromCharCode() method to convert ASCII codes to characters. We can also convert strings to ASCII arrays and ASCII arrays to strings. These conversion operations are very simple, but very useful in programming and can be used in many scenarios.
The above is the detailed content of How to convert characters and ascii codes to each other in javascript. For more information, please follow other related articles on the PHP Chinese website!