469,282 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Convert from Entity Reference to Hex or Decimal?

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
Jul 23 '05 #1
2 4493
Fox


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).toUpperC ase();
}

alert("ã = &#" + entity2CharCode("ã"));
alert("ð = %" + entity2Hex("ð") +
" (" + entity2CharCode("ð") + ")");
You should implement error checking...

Fox
***************

Jul 23 '05 #2
On Sat, 18 Sep 2004 04:00:42 -0500, Fox <fo*@fxmahoney.com> 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).toUpperC ase();
}

alert("&atilde; = &#" + entity2CharCode("&atilde;"));
alert("&eth; = %" + entity2Hex("&eth;") +
" (" + entity2CharCode("&eth;") + ")");
You should implement error checking...

Fox
***************

works great! (mozilla, IE; on XP) thanks!
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ed Dennison | last post: by
11 posts views Thread by Douglas Reith | last post: by
38 posts views Thread by Haines Brown | last post: by
6 posts views Thread by Tuomas Rannikko | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.