奇怪的是,没有人指出如何计算一个Unicode字符占用多少字节。以下是UTF-8编码字符串的规则:
Binary Hex Comments
0xxxxxxx 0x00..0x7F Only byte of a 1-byte character encoding
10xxxxxx 0x80..0xBF Continuation bytes (1-3 continuation bytes)
110xxxxx 0xC0..0xDF First byte of a 2-byte character encoding
1110xxxx 0xE0..0xEF First byte of a 3-byte character encoding
11110xxx 0xF0..0xF4 First byte of a 4-byte character encoding
所以快速回答是:它需要1到4个字节,具体取决于第一个字节,它将指示它将占用多少字节。
更新
正如prewett所指出的,此规则仅适用于UTF-8