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?

    Jas
     
    Jason, Jan 26, 2007
    #1
    1. Advertising

  2. Hi Jason.

    Jason wrote:
    > 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.)

    --
    Cameron McCormack, http://mcc.id.au/
    xmpp: â–ª ICQ 26955922 â–ª MSN
     
    Cameron McCormack, Jan 26, 2007
    #2
    1. Advertising

  3. Jason

    Jason Guest

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

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

    Jas

    On Jan 26, 2:49 pm, Cameron McCormack <> wrote:
    > Hi Jason.
    >
    > Jason wrote:
    > > 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.)
    >
    > --
    > Cameron McCormack,http://mcc.id.au/
    >         xmpp:  ▪  ICQ 26955922  ▪  MSN
     
    Jason, Jan 27, 2007
    #3
  4. Jason

    denisb Guest

    Jason <> wrote:
    > On Jan 26, 2:49 pm, Cameron McCormack <> wrote:
    > > 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>


    --
    @@@@@
    E -00 comme on est very beaux dis !
    ' `) /
    |\_ =="
     
    denisb, Jan 27, 2007
    #4
  5. Jason

    Jason Guest

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

    Jas

    On Jan 27, 7:29 pm, (denisb) wrote:
    > Jason <> wrote:
    > > On Jan 26, 2:49 pm, Cameron McCormack <> wrote:
    > > > 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 !
    > ' `) /
    > |\_ =="
     
    Jason, Jan 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,903
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    917
    raavi
    Mar 2, 2006
  3. cgbusch
    Replies:
    6
    Views:
    7,525
    Mike Brown
    Sep 2, 2003
  4. Kenneth McDonald
    Replies:
    1
    Views:
    856
    Carl Banks
    Dec 27, 2006
  5. Tyler
    Replies:
    1
    Views:
    977
    Robert Klemme
    Jul 29, 2011
Loading...

Share This Page