Getting Unicode Character Codes in JavaScript?
jargonCCNA asks: "I've searched high and low across the web, but I can't seem to be able to find any code snippets or even anything that'll help me out here. I'm trying to get a Unicode character code from a data stream in JavaScript and there doesn't seem to be anything out there to help me; JavaScript itself only has onboard support for ISO-Latin_1, or something. I tried hacking my own converter code, but it's rife with errors. Anybody know of some code that I can include in a GPL project?"
Just for reference -- I've seen a lot of people get nailed on Ask /. because they didn't do the proper research before asking their question. Google has failed me; I've been trying to figure this out on my own for about a month. I hope someone can shed some light on my situation."
"Here's the buggy code, if you're interested:
function unicode2hex( unicode )"Mozilla's JavaScript console lets me know that '\u0' is an illegal character. I think this would work if I could make it use the string "0000" instead of the number 0 for i.
{var hexString = "";}
for( var i = 0x0000; i <= 0xFFFF; i++ )
{test = eval( "\\u" + i );}
if ( unicode == test )
{hexString += i / 4096;}
hexString += i / 256;
hexString += i / 16;
hexString += i % 16;
hexString += "";
return hexString;
return false;
Just for reference -- I've seen a lot of people get nailed on Ask /. because they didn't do the proper research before asking their question. Google has failed me; I've been trying to figure this out on my own for about a month. I hope someone can shed some light on my situation."
How did this story get past the lameness filter?
What's the deal? Cliff must have hit the "Accept" instead of the "Reject" button by accident.
Try asking your question in IRC before hitting up "Ask Slashdot."
A search on google for unicode and javascript brings back a lot of positive looking results without actually delving into them. It seems like JS1.5 has support for this (from the Google summaries).
Marques Johansson
No offense, but I haven't used JS in years, and I found this in a matter of minutes.
document.write("\u00A9 is ");
document.write("\u00A9".charCodeAt(0));
That will give you the answer in decimal. I trust you can convert to hex yourself.
(Note: Requires Javascript 1.3; previous versions used ISO-Latin-1 rather than unicode, and I don't know what they'd do with a character higher than 255.)
The most valuable commodity I know of is information. - Michael Douglas as Gordon Gekko, Wall Street