EDIT: Ooooops.
console.log (Buffer.byteLength ("", "utf8")); //--> 3
I want to count the number of bytes of a character. This is what I have:
var charBytesLength = function (c){
var n = 0;
c = c.charCodeAt (0);
do{
c = c >>> 8;
n++;
}while (c);
return n;
};
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 charCodeAt()
only returns the value between 0 and 65535.
For example:
"".charCodeAt (0)
returns 65533, when the real hexadecimal value is 0x24065 (147557 in decimal)
Do you know how to count the real number of bytes?
console.log (Buffer.byteLength ("", "utf8")); //--> 3