Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 260
k.ken Avatar asked Jun 24 '13 02:06

k.ken


People also ask

How do you convert a string with Unicode encoding to a string of letters?

String str1 = "\u0000"; String str2 = "\uFFFF"; String str1 is assigned \u0000 which is the lowest value in Unicode. String str2 is assigned \uFFFF which is the highest value in Unicode.

Can Java strings handle Unicode character strings?

Internally in Java all strings are kept in Unicode. Since not all text received from users or the outside world is in unicode, your application may have to convert from non-unicode to unicode.

Is Unicode the same as string?

Unicode is a standard encoding system that is used to represent characters from almost all languages. Every Unicode character is encoded using a unique integer code point between 0 and 0x10FFFF . A Unicode string is a sequence of zero or more code points.

How do I convert Unicode to Ascii?

You CAN'T convert from Unicode to ASCII. Almost every character in Unicode cannot be expressed in ASCII, and those that can be expressed have exactly the same codepoints in ASCII as in UTF-8, which is probably what you have.


2 Answers

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.

like image 81
Bryan Rayner Avatar answered Sep 30 '22 09:09

Bryan Rayner


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.

like image 32
k.ken Avatar answered Sep 30 '22 11:09

k.ken