Follow Slashdot stories on Twitter

 



Forgot your password?
typodupeerror
×
Java Programming

Getting Unicode Character Codes in JavaScript? 26

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?"

"Here's the buggy code, if you're interested:

function unicode2hex( unicode )

{
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;
}
"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.

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."
This discussion has been archived. No new comments can be posted.

Getting Unicode Character Codes in JavaScript?

Comments Filter:
  • by Lazarus Short ( 248042 ) on Thursday August 01, 2002 @07:31PM (#3995620) Homepage
    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.)

New York... when civilization falls apart, remember, we were way ahead of you. - David Letterman

Working...