Javascript unicode

From John Freier
Jump to: navigation, search

Here is some JavaScript code to convert characters to unicode.

function toUnicode(theString) {
  var unicodeString = ;
  for (var i=0; i < theString.length; i++) {
    var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
    while (theUnicode.length < 4) {
      theUnicode = '0' + theUnicode;
    }
    theUnicode = '\\u' + theUnicode;
    unicodeString += theUnicode;
  }
  return unicodeString;
}


or

Here is a short version, just remember to make sure the length count is 4, adding front 0's

alert("o".charCodeAt(0).toString(16))