7

In theory, no matter what the input is, the output should be unchanged:

String.fromCharCode("a".charCodeAt(0));    //"a"

This makes sense because I'm just trying to get the char code of a character, and then casting it back to a character.

However when I try with this character, it breaks:

//"": 55356
String.fromCharCode("".charCodeAt(0));    //"�" (65533)

enter image description here

(Note that I actually highlighted the string and pasted it into the next line. It changed to by itself for some reason.)

Why is this happening and how can I fix this?

I noticed that there is a new method in ES6, String.fromCodePoint() but it is not supported every browser except Firefox.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • @user1281385 - Wow, that's in Java, not JavaScript. – Derek 朕會功夫 Mar 12 '14 at 07:02
  • Sorry yeah I was trying to delete when you commented – exussum Mar 12 '14 at 07:03
  • Probably a better link http://stackoverflow.com/questions/5446492/unicode-characters-from-charcode-in-javascript-for-charcodes-0xffff – exussum Mar 12 '14 at 07:07
  • Try console out `String.fromCharCode("".charCodeAt(0)).charCodeAt(0)` -> http://jsfiddle.net/9bLxB/ – davidkonrad Mar 12 '14 at 07:11
  • Are you sure your files are UTF-8 encoded? Depending on the dev environment and your settings, that may not be the case. (Might want to go ahead and edit your tags, too.) –  Mar 12 '14 at 07:13
  • @JeremyMiller - I don't know since this is actually a part of my script in [Tampermonkey](http://tampermonkey.net/). I'm trying to insert `` into the document but it comes out as the "replacement character" as [xdazz](http://stackoverflow.com/users/762073/xdazz) mentioned. Then I tried saving the script with their charCode and converting it back later. Now here's how I get into this problem. Whenever I try to put this character into the document it changes to `�`. – Derek 朕會功夫 Mar 12 '14 at 07:19
  • Haven't worked with that environment before, but I'd check encoding for sure. Everytime I've had such an issue it's because of encoding... DB, webpage, Java, or otherwise. –  Mar 12 '14 at 07:20
  • @JeremyMiller - After doing some experiments, it seems like if I directly do `inputelement.value = ""` it works. But when I store it in an object `{"a":""}` it doesn't return the correct character. – Derek 朕會功夫 Mar 12 '14 at 07:31
  • Wow. I'd add that to the main part of your post b/c it's def informative. Unfortunately, that also takes it out of my experience on these issues. Only time I've seen such behavior is when the function isn't UTF-8 compat, but an object isn't a function, lol! –  Mar 12 '14 at 07:33
  • 1
    Still, `console.log("".charCodeAt(0), String.fromCharCode("".charCodeAt(0)).charCodeAt(0));` gives 55356 55356 http://jsfiddle.net/9bLxB/1/ so what is the problem really,, it does not break? – davidkonrad Mar 12 '14 at 07:33
  • @davidkonrad - Yes, I realized that there is nothing wrong with this part. Perhaps I should reformulate my question. – Derek 朕會功夫 Mar 12 '14 at 07:34

1 Answers1

4

It does not change.

> String.fromCharCode("".charCodeAt(0)).charCodeAt(0)
  55356

And you can check all by:

for (var i = 0; i <= 65535; i++) {
   if (String.fromCharCode(i).charCodeAt(0) !== i) {
       console.log('error', i, String.fromCharCode(i)); 
   }
}

Why you saw is because (65533) is used to replace a character whose value is unknown or unrepresentable in unicode.

It is called REPLACEMENT CHARACTER.

xdazz
  • 158,678
  • 38
  • 247
  • 274