首页 > 解决方案 > 如何检查新角色的现有角色的延续?

问题描述

如果您正在读取编码为 UTF-8 的文件,并且您读取了一个字节,那么您如何确定该字节是否是现有字符的延续,而不是新字符的开头?

标签: utf-8

解决方案


如果字节的二进制值为10xxxxxxx可以是 0 或 1),则该字节是 UTF-8 连续字节。初始 UTF-8 字节遵循以下模式:

0xxxxxxx - start (and end) of 1-byte sequence
110xxxxx - start of 2-byte sequence (followed by one continuation byte)
1110xxxx - start of 3-byte sequence (followed by two continuation bytes)
11110xxx - start of 4-byte sequence (followed by three continuation bytes)

推荐阅读