67

how can i convert ascii code to character in javascript

1
  • 1
    JavaScript "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? Commented Feb 9, 2017 at 17:38

3 Answers 3

129
String.fromCharCode(ascii_code)
Sign up to request clarification or add additional context in comments.

2 Comments

character.charCodeAt(0), for example, 'a'.charCodeAt(0)
16

If you want to convert Ascii Codes to Characters you can use:

String.fromCharCode(codes);

For Example:

String.fromCharCode(65,66,67);

which returns = "ABC"

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.