unicode to character

J

Jason

This is a Chinese character in unicode: 挪
I made it in Javascript by adding "&#"+"25"+"386"
I need to convert it in Javascript to this: 挪 (The actual character)
How do I achieve this conversion in Javascript?

Jas
 
C

Cameron McCormack

Hi Jason.
This is a Chinese character in unicode: 挪
I made it in Javascript by adding "&#"+"25"+"386"
I need to convert it in Javascript to this: 挪 (The actual character)
How do I achieve this conversion in Javascript?

挪 isn't the character in Unicode, it's the numeric character
entity in XML and HTML that means that character. 25386 is the Unicode
codepoint of that character. You can use the String.fromCharCode
function to create a string with a single character, given the codepoint.

If you have the codepoint, just pass it to the function. If you have
the string "挪", you need to extract the number from the string
and then pass it to the function. You can use something like:

function numericEntityToChar(s) {
return String.fromCharCode(s.match(/^&#(\d+);$/)[1]);
}

If you have a numeric character entity that uses hexadecimal, you'll
have to do it differently, e.g.:

function numericEntityToChar(s) {
var xs = s.match(/^&#(x[0-9A-Fa-z]+);$/));
if (xs) {
return Number('0' + xs[1]);
}
return Number(s.match(/^&#(\d+);$/)[1]);
}

(Completely untested code, though.)
 
J

Jason

Couldn't get it working. Here is a test page:

http://dheera.net/jason/test.html

Jas

Hi Jason.
This is a Chinese character inunicode: 挪
I made it in Javascript by adding "&#"+"25"+"386"
I need to convert it in Javascript to this: 挪 (The actual character)
How do I achieve this conversion in Javascript?挪 isn't the character inUnicode, it's the numeric character
entity in XML and HTML that means that character.  25386 is theUnicode
codepoint of that character.  You can use the String.fromCharCode
function to create a string with a single character, given the codepoint.

If you have the codepoint, just pass it to the function.  If you have
the string "挪", you need to extract the number from the string
and then pass it to the function.  You can use something like:

  function numericEntityToChar(s) {
    return String.fromCharCode(s.match(/^&#(\d+);$/)[1]);
  }

If you have a numeric character entity that uses hexadecimal, you'll
have to do it differently, e.g.:

  function numericEntityToChar(s) {
    var xs = s.match(/^&#(x[0-9A-Fa-z]+);$/));
    if (xs) {
      return Number('0' + xs[1]);
    }
    return Number(s.match(/^&#(\d+);$/)[1]);
  }

(Completely untested code, though.)
 
D

denisb

Jason said:
If you have the codepoint, just pass it to the function.
If you have the string "挪", you need to extract
the number from the string and then pass it to the function.
You can use something like:
function numericEntityToChar(s) {
return String.fromCharCode(s.match(/^&#(\d+);$/)[1]);
}
Couldn't get it working. Here is a test page:
http://dheera.net/jason/test.html

try :
<script type="text/javascript">
//<![CDATA[
function numericEntityToCha(s) {
return String.fromCharCode(s.match(/^&#(\d+);$/ )[1] );
}

function numericEntityToChar(s) {
var xs = s.match(/^&#(x[0-9A-Fa-z]+);$/ );
if (xs) {
return Number('0' + xs[1] );
}
return Number(s.match(/^&#(\d+);$/ )[1] );
}

document.write(numericEntityToCha('挤' ) );
document.write(numericEntityToChar('挤' ) );
document.write("asd" );
//]]>
</script>
 
J

Jason

Cameron, Denisb thanks so much for your help. Much appreciated. :)

Jas

Jason said:
If you have the codepoint, just pass it to the function.
If you have the string "挪", you need to extract
the number from the string and then pass it to the function.
You can use something like:
function numericEntityToChar(s) {
return String.fromCharCode(s.match(/^&#(\d+);$/)[1]);
}
Couldn't get it working. Here is a test page:
http://dheera.net/jason/test.htmltry :
<script type="text/javascript">
//<![CDATA[
function numericEntityToCha(s) {
return String.fromCharCode(s.match(/^&#(\d+);$/ )[1] );

}function numericEntityToChar(s) {
var xs = s.match(/^&#(x[0-9A-Fa-z]+);$/ );
if (xs) {
return Number('0' + xs[1] );
}
return Number(s.match(/^&#(\d+);$/ )[1] );

}document.write(numericEntityToCha('挤' ) );
document.write(numericEntityToChar('挤' ) );
document.write("asd" );
//]]>
</script>

--
@@@@@
E -00 comme on est very beaux dis !
' `) /
|\_ =="
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top