12

I've never done this before and am not sure why it's outputting the infamous encoding character. Any ideas on how to output characters as they should (ASCII+Unicode)? I think \u0041-\u005A should print A-Z in UTF-8, which Firefox is reporting is the page encoding.

   var c   = new Array("F","E","D","C","B","A",9,8,7,6,5,4,3,2,1,0);
   var n   = 0;
   var d   = "";
   var o   = "";

   for (var i=16;i--;){
      for (var j=16;j--;){
         for (var k=16;k--;){
            for (var l=16;l--;){

               d =  c[i].toString()
                 +  c[j].toString()
                 +  c[k].toString()
                 +  c[l].toString();

               o += ( ++n + ": " 
                    + d   + " = " 
                    + String.fromCharCode("\\u" + d) 
                    + "\n<br />" );

               if(n>=500){i=j=k=l=0;} // stop early
            }
         }
      }
   }

   document.write(o);
vol7ron
  • 40,809
  • 21
  • 119
  • 172

3 Answers3

22

The .fromCharCode() function takes a number, not a string. You can't put together a string like that and expect the parser to do what you think it'll do; that's just not the way the language works.

You could ammend your code to make a string (without the '\u') from your hex number, and call

var n = parseInt(hexString, 16);

to get the value. Then you could call .fromCharCode() with that value.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I'll have to try when I get home. For some reason I thought I remember someone building a string and using fromCharCode on it. – vol7ron Sep 30 '10 at 22:58
  • So basically, `\u` is only needed in open code and `fromCharCode` will convert integers into unicode characters, not just ASCII characters – vol7ron Oct 02 '10 at 23:26
11

A useful snippet for replacing all unicode-encoded special characters in a text is:

var rawText = unicodeEncodedText.replace(
                  /\\u([0-9a-f]{4})/g, 
                  function (whole, group1) {
                      return String.fromCharCode(parseInt(group1, 16));
                  }
              );

Using replace, fromCharCode and parseInt

Matyas
  • 13,473
  • 3
  • 60
  • 73
3

If you want to use the \unnnn syntax to create characters, you have to do that in a literal string in the code. If you want to do it dynamically, you have to do it in a literal string that is evaluated at runtime:

var hex = "0123456789ABCDEF";
var s = "";
for (var i = 65; i <= 90; i++) {
  var hi = i >> 4;
  var lo = i % 16;
  var code = "'\\u00" + hex[hi] + hex[lo] + "'";
  var char = eval(code);
  s += char;
}
document.write(s);

Of course, just using String.fromCharCode(i) would be a lot easier...

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I tried using a form of eval, which didn't work; I can't really remember what I did. Perhaps it's because I was printing to the console at the time. I see you did the range from 65..90; is there any range for that? - like I said, I'm wanting to see beyond just the ASCII, which I think stops at 255. – vol7ron Oct 02 '10 at 23:29
  • Why the downvote? If you don't explain what you think is wrong, it can't improve the answer. – Guffa Apr 09 '13 at 22:45
  • @vol7ron: Yes, I imagine you did two years ago, but someone downvoted yesterday. – Guffa Apr 10 '13 at 06:40