unicode to character

Discussion in 'Javascript' started by Jason, Jan 26, 2007.

  1. Jason

    Jason Guest

    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?

    Jason, Jan 26, 2007
    1. Advertisements

  2. Hi Jason.
    挪 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.)
    Cameron McCormack, Jan 26, 2007
    1. Advertisements

  3. Jason

    Jason Guest

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



    Jason, Jan 27, 2007
  4. Jason

    denisb Guest

    try :
    <script type="text/javascript">
    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" );
    denisb, Jan 27, 2007
  5. Jason

    Jason Guest

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


    Jason, Jan 27, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.