How to get the string length in bytes in nodejs? If I have a string, like this: äáöü
then str.length will return with 4. But how to get that, how many bytes form the string?
Thanks in advance
So a string size is 18 + (2 * number of characters) bytes. (In reality, another 2 bytes is sometimes used for packing to ensure 32-bit alignment, but I'll ignore that). 2 bytes is needed for each character, since . NET strings are UTF-16.
The size of a JavaScript string is. Always 2 bytes per character.
Here is an example:
str = 'äáöü'; console.log(str + ": " + str.length + " characters, " + Buffer.byteLength(str, 'utf8') + " bytes"); // äáöü: 4 characters, 8 bytes
Buffer.byteLength(string, [encoding])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With