Convert from Entity Reference to Hex or Decimal?

Discussion in 'Javascript' started by kurt, Sep 18, 2004.

  1. kurt

    kurt Guest

    hello,
    Is there a way to convert entity references ( "ã",etc)
    to their equivalent hex or decimal codes without using an associative
    array or some such thing to list all the equivalencies?
    thanks,
    kurt
    kurt, Sep 18, 2004
    #1
    1. Advertising

  2. kurt

    Fox Guest

    kurt wrote:
    > hello,
    > Is there a way to convert entity references ( "ã",etc)
    > to their equivalent hex or decimal codes without using an associative
    > array or some such thing to list all the equivalencies?
    > thanks,
    > kurt


    These might work:

    var gDiv = null;

    function
    entity2CharCode(ent)
    {
    var retval = null;

    if(!gDiv)
    gDiv = document.createElement('DIV');

    gDiv.innerHTML = ent;

    return gDiv.innerHTML.charCodeAt(0);
    }

    function
    entity2Hex(ent)
    {

    if(!gDiv)
    gDiv = document.createElement('DIV');

    gDiv.innerHTML = ent;

    return gDiv.innerHTML.charCodeAt(0).toString(16).toUpperCase();
    }

    alert("ã = &#" + entity2CharCode("ã"));
    alert("ð = %" + entity2Hex("ð") +
    " (" + entity2CharCode("ð") + ")");


    You should implement error checking...

    Fox
    ***************
    Fox, Sep 18, 2004
    #2
    1. Advertising

  3. kurt

    kurt Guest

    On Sat, 18 Sep 2004 04:00:42 -0500, Fox <> wrote:
    >
    >
    >kurt wrote:
    >> hello,
    >> Is there a way to convert entity references ( "&atilde;",etc)
    >> to their equivalent hex or decimal codes without using an associative
    >> array or some such thing to list all the equivalencies?
    >> thanks,
    >> kurt

    >
    >These might work:
    >
    >var gDiv = null;
    >
    >function
    >entity2CharCode(ent)
    >{
    >var retval = null;
    >
    > if(!gDiv)
    > gDiv = document.createElement('DIV');
    >
    > gDiv.innerHTML = ent;
    >
    > return gDiv.innerHTML.charCodeAt(0);
    >}
    >
    >function
    >entity2Hex(ent)
    >{
    >
    > if(!gDiv)
    > gDiv = document.createElement('DIV');
    >
    > gDiv.innerHTML = ent;
    >
    > return gDiv.innerHTML.charCodeAt(0).toString(16).toUpperCase();
    >}
    >
    >alert("&atilde; = &#" + entity2CharCode("&atilde;"));
    >alert("&eth; = %" + entity2Hex("&eth;") +
    > " (" + entity2CharCode("&eth;") + ")");
    >
    >
    >You should implement error checking...
    >
    >Fox
    >***************



    works great! (mozilla, IE; on XP) thanks!
    kurt, Sep 19, 2004
    #3
    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. Replies:
    10
    Views:
    6,191
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    466
    Juha Autero
    Aug 19, 2003
  3. convert hex to decimal

    , Mar 9, 2006, in forum: Python
    Replies:
    2
    Views:
    2,420
  4. markla
    Replies:
    1
    Views:
    545
    Steven Cheng
    Oct 6, 2008
  5. chirs
    Replies:
    3
    Views:
    163
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page