how can i convert ascii code to character in javascript
-
1JavaScript "characters" (like in many modern languages) are UTF-16 code units. If for some strange reason you have an ASCII code unit, you can easily convert it by relying on 1) ASCII code unit are the same as ASCII codepoint, 2) ASCII codepoints are the same as Unicode codepoints (for the characters in common), 3) UTF-16 encodes those characters in one UTF-16 code unit, and 4) those UTF-16 code units, although having 16 bits has the same integer value as the ASCII code units. Or, did you really just have a UTF-16 code unit are for some reason calling it ASCII?Tom Blodget– Tom Blodget2017-02-09 17:38:48 +00:00Commented Feb 9, 2017 at 17:38
Add a comment
|
3 Answers
String.fromCharCode(ascii_code)
2 Comments
Elangovan
Darko Kenda
character.charCodeAt(0), for example, 'a'.charCodeAt(0)
If you are going to convert the array of ASCII codes
You can easily convert an array of ASCII codes back to a string in JavaScript using the String.fromCharCode() method. Here's a simple function for this purpose:
const ASCIIToTextConvert = (asciiArray) => {
return String.fromCharCode(...asciiArray);
};
const asciiArray = [72, 101, 108, 108, 111];
const textString = ASCIIToTextConvert(asciiArray);
console.log(textString); // Output: "Hello"
To verify your output you can use an online ascii to text converter.