Follow Slashdot blog updates by subscribing to our blog RSS feed

 



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:

You knew the job was dangerous when you took it, Fred. -- Superchicken

Working...