Converting unicode character to string format

Does anyone know how to convert a unicode to a string in javascript. For instance:

\u2211 -> ? \u0032 -> 2 \u222B -> ?

I basically want to be able to display the symbol in xhtml or html. Haven't decided which I will be using yet.

Answers:

Answer

A function from k.ken's response:

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

Takes all unicode characters in the inputted string, and converts them to the character.

Answer

Just found a way: String.fromCharCode(parseInt(unicode,16)) returns the right symbol representation. The unicode here doesn't have the \u in front of it just the number.

Answer

To convert an given Unicode-Char like ? to String-Representation, you can also use this oneliner:

var unicodeToStr = '?'.codePointAt(0).toString(16)

The above example gives you 'F21D'. Used with fontAwesome, you get street-view Icon: '\F21D'

Answer

Another way:

const unicodeText = "F1A3";
let unicodeChar = JSON.parse(`["\\u${unicodeText}"]`)[0];
Answer
var string = '/0004';  // One of unicode
var unicodeToStr = string.codePointAt(0).toString(16)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.