首页 > 解决方案 > UTF-8 可以在中间包含零字节吗?

问题描述

UTF-8 对每个字符使用 1-4 个字节。
这是Python 2len('') == 4和 JavaScript中的 4 字节字符So encodeURI('') === "%F0%9F%90%8D"
问题是,UTF-8 可以在中间包含一个零字节吗?

例如,第一个俄语字母А由 2 个字节组成:0xD0, 0x90.
可能存在一个字母中间没有前导零或零的字母,像这样0xAB, 0, 0xCD

标签: encodingutf-8character-encoding

解决方案


The only zero byte in a valid UTF-8 stream would be a representation of U+0000 NULL, which is just 00 (hex) in UTF-8.

No valid encoding of any other character in UTF-8 will produce a full byte without any bits set.

In other words: if your input characters does not contain the NULL character, then your output byte stream is guaranteed to not contain any zero bytes.


推荐阅读