Slashdot Mirror


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

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

5 of 26 comments (clear)

  1. One question by Henry+V+.009 · · Score: 5, Funny

    How did this story get past the lameness filter?

    1. Re:One question by Real+World+Stuff · · Score: 2

      Desperation for Quality content.

      --
      If we don't fight for ourselves no one will.
  2. Isn't this a question for developer.net? by displague · · Score: 2

    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
  3. Did you try looking at the docs? by Lazarus+Short · · Score: 5, Informative

    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
    1. Re:Did you try looking at the docs? by josepha48 · · Score: 2
      Here is something that will convert:
      function tounicode(instr) {
      len = instr.length;
      switch (len) {
      case 1:
      return instr.charCodeAt(0);
      case 2:
      return new String(instr.charCodeAt(1)) + new String(instr.charCodeAt(0));
      case 3:
      return instr.charCodeAt(2) + instr.charCodeAt(1) + instr.charCodeAt(0);
      case 4:
      return instr.charCodeAt(3) + instr.charCodeAt(2) + instr.charCodeAt(1) + instr.charCodeAt(0);
      }
      return "";
      }

      document.write(tounicode("\u002d") + " " + tounicode("-") + "
      ");

      With this you can take a string like "fooo" with a unicode equivalant.

      --

      Only 'flamers' flame!