简体   繁体   中英

String encoding in javascript UTF-16 or UTF-8

While reading charAt in MDN, I've found that strings in javascript sequence of UTF-16?

So to verify my understanding, I've tried the following script in NodeJS

Buffer.from("€").length // gives 3

How can this be 3 bytes? As far as I know, this should be either 2 or 4 bytes, How can UTF-16 string give 3 bytes?

I've looked at different questions here in StackOverflow but didn't find an explanation for this part.

Because when creating a Buffer.from a string , it needs an encoding, and if you don't specify one, it defaults to UTF-8.

You seem to be looking for either

Buffer.from("€", "utf16le").length

or just

"€".length * 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM