Skip to content

Instantly share code, notes, and snippets.

@dbodyas
Created February 12, 2012 15:30
Show Gist options
  • Save dbodyas/1809069 to your computer and use it in GitHub Desktop.
Save dbodyas/1809069 to your computer and use it in GitHub Desktop.
JavaScript Get Unicode char as decimal
/*
Sometimes, unicode symbols that have to be inserted in an HTML page should be escaped.
For example, everybody know, that’s “&” should be escaped as “&”, ” ” as “ ” and so on.
But what, if you should insert symbol, that you never used before. So you don’t know how it should be present or escaped.
Code below, helps you to get valid decimal unicode for appropriate symbol.
*/
function getCharAsDecimalUTF8(char){
var getUTF8 = escape(char);
console.log("&#" + parseInt(getUTF8.substring(getUTF8.match(/\%u/) !== null ? 2 : 1), 16) + ";");
}
// Ɓ †
getCharAsDecimalUTF8("†"); // †
@omarwagih
Copy link

Awesome snippet, very useful!

@marcellodesales
Copy link

Awesome to implement the Turkish I problem test...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment