title: How to count the number of bytes of a character
body: <p>EDIT: Ooooops.</p>
  
  <pre><code>console.log (Buffer.byteLength ("", "utf8")); //--&gt; 3
  </code></pre>
  
  <hr>
  
  <p>I want to count the number of bytes of a character. This is what I have:</p>
  
  <pre><code>var charBytesLength = function (c){
      var n = 0;
      c = c.charCodeAt (0);
      do{
          c = c &gt;&gt;&gt; 8;
          n++;
      }while (c);
      return n;
  };
  </code></pre>
  
  <p>If the character is encoded using UTF8, for characters with more than 2 bytes the function always returns 2 because in Javascript a character is a 16-bit value and <code>charCodeAt()</code> only returns the value between 0 and 65535.</p>
  
  <p>For example:</p>
  
  <pre><code>"".charCodeAt (0)
  </code></pre>
  
  <p>returns 65533, when the real hexadecimal value is 0x24065 (147557 in decimal)</p>
  
  <p>Do you know how to count the real number of bytes?</p>