Javascript, handling Unicode code

Asked

Viewed 157 times

1

How can I print the character of a Unicode code?

For example:

var i = "\u0062";

How do I convert this code to the character it represents?

1 answer

3


Simply print it out:

<script>
    var i = "\u0062";
    document.write(i); // imprime: b
</script>

"- How do I convert this code to the character it represents?"

This "conversion" is done automatically. It is very normal to see code written like this after being obfuscated. It has even been done a question about Javascript code obfuscation here.


But if, for some good reason, you want to convert "at hand", you have this little function that can help you:

function unicodeToChar(text) {
   return text.replace(/\\u[\dA-F]{4}/gi,
          function (match) {
               return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
          });
}

var i = "\u0062";

document.write(unicodeToChar(i));

Source: response from Bryan Rayner in the Soen: Converting Nicode Character to string format (Ctrl+C > Ctrl+V).

Recommended reading: MDN - String.fromCharCode();


I thought it would be interesting to share, too, this table that I use a lot in my queries: Rapidtable - Unicode characters table;

  • How it would be in string?

  • @Linhadecode Opens this documentation that posted the link. You just pass the numeric codes of each comma-separated character and the method returns the converted string. Use the function I posted as a template.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.