UTF-8、UTF-16和UTF-32之间有什么区别?

我知道它们都将存储Unicode,并且每个都使用不同的字节数来表示一个字符。选择一个比另一个有优势吗?


当前回答

Unicode是一个标准,关于UTF-x,你可以把它看作是一个技术实现,用于一些实际目的:

UTF-8 - "size optimized": best suited for Latin character based data (or ASCII), it takes only 1 byte per character but the size grows accordingly symbol variety (and in worst case could grow up to 6 bytes per character) UTF-16 - "balance": it takes minimum 2 bytes per character which is enough for existing set of the mainstream languages with having fixed size on it to ease character handling (but size is still variable and can grow up to 4 bytes per character) UTF-32 - "performance": allows using of simple algorithms as result of fixed size characters (4 bytes) but with memory disadvantage

其他回答

简而言之:

UTF-8: Variable-width encoding, backwards compatible with ASCII. ASCII characters (U+0000 to U+007F) take 1 byte, code points U+0080 to U+07FF take 2 bytes, code points U+0800 to U+FFFF take 3 bytes, code points U+10000 to U+10FFFF take 4 bytes. Good for English text, not so good for Asian text. UTF-16: Variable-width encoding. Code points U+0000 to U+FFFF take 2 bytes, code points U+10000 to U+10FFFF take 4 bytes. Bad for English text, good for Asian text. UTF-32: Fixed-width encoding. All code points take four bytes. An enormous memory hog, but fast to operate on. Rarely used.

长:参见维基百科:UTF-8, UTF-16和UTF-32。

在UTF-32中,所有字符都用32位编码。这样做的好处是可以很容易地计算字符串的长度。缺点是对于每个ASCII字符,您会浪费额外的3个字节。

在UTF-8字符有可变长度,ASCII字符编码为一个字节(8位),大多数西方特殊字符编码为两个字节或三个字节(例如€是三个字节),更奇特的字符可以占用四个字节。明显的缺点是,先验你不能计算字符串的长度。但与UTF-32相比,编码拉丁(英语)字母文本所需的字节要少得多。

UTF-16也是可变长度的。字符编码为两个字节或四个字节。我真的不明白这有什么意义。它有可变长度的缺点,但没有像UTF-8那样节省空间的优点。

在这三种语言中,UTF-8显然是传播最广泛的。

Unicode是一个标准,关于UTF-x,你可以把它看作是一个技术实现,用于一些实际目的:

UTF-8 - "size optimized": best suited for Latin character based data (or ASCII), it takes only 1 byte per character but the size grows accordingly symbol variety (and in worst case could grow up to 6 bytes per character) UTF-16 - "balance": it takes minimum 2 bytes per character which is enough for existing set of the mainstream languages with having fixed size on it to ease character handling (but size is still variable and can grow up to 4 bytes per character) UTF-32 - "performance": allows using of simple algorithms as result of fixed size characters (4 bytes) but with memory disadvantage

UTF-8为变量1 ~ 4字节。 UTF-16是可变的2或4字节。 UTF-32是固定的4字节。

utf - 8

没有字节顺序的概念 每个字符使用1到4个字节 ASCII是一种兼容的编码子集 完全自同步,例如从流中的任何地方删除字节最多只会损坏一个字符 几乎所有的欧洲语言都是用两个字节或更少的字符编码的

utf - 16

必须使用已知的字节顺序进行解析,或者读取字节顺序标记(BOM)。 每个字符使用2或4个字节

utf - 32

每个字符是4个字节 必须使用已知的字节顺序进行解析,或者读取字节顺序标记(BOM)。

UTF-8将是空间效率最高的,除非大多数字符来自CJK(中国、日本和韩国)字符空间。

UTF-32最适合通过字符偏移量随机访问字节数组。