How does UTF16 encode characters?

2019-09-22 07:40发布

EDIT

Since it seems I'm not going to get an answer to the general question. I'll restrict it to one detail: Is my understanding of the following, correct?

That surrogates work as follows:

  1. If the first pair of bytes is not between D800 and DBFF - there will not be a second pair.
  2. If it is between D800 and DBFF - a) there will be a second pair b) the second pair will be in the range of DC00 and DFFF.
  3. There is no single pair UTF16 character with a value between D800 and DBFF.
  4. There is no single pair UTF16 character with a value between DC00 and DFFF.

Is this right?

Original question

I've tried reading about UTF16 but I can't seem to understand it. What are "planes" and "surrogates" etc.? Is a "plane" the first 5 bits of the first byte? If so, then why not 32 planes since we're using those 5 bits anyway? And what are surrogates? Which bits do they correspond to?

I do understand that UTF16 is a way to encode Unicode characters, and that it sometimes encodes characters using 16 bits, and sometimes 32 bits, no more no less. I assume that there is some list of values for the first 2 bytes (which are the most significant ones?) which indicates that a second 2 bytes will be present.

But instead of me going on about what I don't understand, perhaps someone can make some order in this?

1条回答
SAY GOODBYE
2楼-- · 2019-09-22 08:05

Yes on all four.

To clarify, the term "pair" in UTF-16 refers to two UTF-16 code units, the first in the range D800-DBFF, the second in DC00-DFFF.

A code unit is 16-bits (2 bytes), typically written as an unsigned integer in hexadecimal (0x000A). The order of the bytes (0x00 0x0A or 0x0A 0x00) is specified by the author or indicated with a BOM (0xFEFF) at the beginning of the file or stream. (The BOM is encoded with the same algorithm as the text but is not part of the text. Once the byte order is determined and the bytes are reordered to the native ordering of the system, it typically is discarded.)

查看更多
登录 后发表回答