0

I have cyrillic word in my js file which charset is UTF-8 without BOM. How can I get hex index of unicode table of my first character? The word "Абв" and result must return hex code "0x0410".

I tried this code, but it returns wrong result:

var code = "А".charCodeAt(0);
var codeHex = code.toString(16).toUpperCase();
console.log(code, codeHex);
Macedonian
  • 47
  • 11

1 Answers1

0

Note that in your console.log(code, codeHex); you have no space between the two values code and codeHex, so you'll get to see a seemingly big value (1040410).

So separate like this:

console.log(code, ' ', codeHex);

and if you want a nice hex formatting, do this:

console.log(code, ' ', '0x' + ('0000' + codeHex).substr(-4));

Snippet:

var code = "А".charCodeAt(0);
var codeHex = code.toString(16).toUpperCase();
document.write(code, ' ', '0x' + ('0000' + codeHex).substr(-4));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Well, when I typing this snippet in console it's work perfect. But when I use it from my file, it always returns: "1056 0x0420". – Macedonian May 13 '16 at 22:18
  • I changed charset of my file to ANSI and it works. But why it does not work with UTF-8? – Macedonian May 13 '16 at 22:22
  • Set the character set of your HTML file: add `` and at the same time save it as UTF-8 encoding (check the character is still the same after doing that as some characters may not survive the switch). – trincot May 13 '16 at 22:26
  • It's ok, I checked this, it's UTF-8. But still not working with UTF-8 – Macedonian May 13 '16 at 22:44
  • Anything else you are doing? I just created such a file from scratch, and it gives the correct result. – trincot May 13 '16 at 23:01
  • I found where is the problem. I didn't tell you that I use iMacros plugin for Firefox, which load another script and run it dynamically. So for correctly work of another script, the iMacros have to recognize the charset UTF-8 with BOM. Anyway thank you for help, I really appreciate it. :) – Macedonian May 14 '16 at 07:07