Skip to content

Convert ASCII value sentence to string JavaScript | Example code

  • by

Traverse the complete string character by character and concatenate every digit and use String.fromCharCode() method Convert ASCII value sentence to string in JavaScript.

Convert ASCII value sentence to string in JavaScript example

HTML example code:

First, inside the function asciiToSentence, we need 2 parameters str and len (str is a string while len is the length of that string).

Next, we make a temporary num to calculate the number inside the string based on this table: ASCII Printable Characters

We trying to parse one-by-one character to a number and multiply it by 10. Then, we compare it between 32 and 122 (based on the number in the table above).

If the number we have is inside the range, we parse that number to a character using String.fromCharCode function and reset the value num. Otherwise, we continue the loop and increase the value num

<!DOCTYPE html>
<html>
<body>
 <script>

  function asciiToSentence(str)
  {
    var num = 0;
    var len = str.length;
    var sent = '';

    for (var i = 0; i < len; i++) {

      num = num * 10 + (str[i] - '0');

      if (num >= 32 && num <= 122) {
        var ch = String.fromCharCode(num);
        sent = sent+ch;
        num = 0;
      }
    }

    return sent;
  }

  console.log(asciiToSentence("7210110810811132119111114108100"));
</script>
</body>
</html>

Output:

Convert ASCII value sentence to string JavaScript

Do comment if you have any doubts and suggestions on this ASCII JS topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *