CancelImage Upload

How to convert a string into ASCII code with JavaScript

To retrieve an individual character's ASCII value the function charCodeAt() can be used, which is the JavaScript equivalent of the PHP function ord():

var obj = 'A';

obj.charCodeAt();

As with the PHP function ord(), charCodeAt() can only convert one symbol at a time. Meaning that if an entire string is provided, only the first symbol, or the symbol specified within the brackets according to position will be converted. However, there is a way around this by running the whole string through a loop:

function fetchAscii(obj)
{

      var convertedObj = '';

      for(i = 0; i < obj.length; i++)
      {

            var asciiChar = obj.charCodeAt(i);

            convertedObj += '&#' + asciiChar + ';';

      }

      return convertedObj;

}

Using this method each character will be translated individually and added to the variable convertedObj.

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com