UTF-8

From Wikipedia for FEVERv2
Jump to navigation Jump to search

UTF-8_table_infobox_0

UTF-8UTF-8_table_caption_0
StandardUTF-8_header_cell_0_0_0 Unicode StandardUTF-8_cell_0_0_1
ClassificationUTF-8_header_cell_0_1_0 Unicode Transformation Format, extended ASCII, variable-width encodingUTF-8_cell_0_1_1
ExtendsUTF-8_header_cell_0_2_0 US-ASCIIUTF-8_cell_0_2_1
Transforms / EncodesUTF-8_header_cell_0_3_0 ISO 10646 (Unicode)UTF-8_cell_0_3_1
Preceded byUTF-8_header_cell_0_4_0 UTF-1UTF-8_cell_0_4_1

UTF-8 is a variable-width character encoding used for electronic communication. UTF-8_sentence_0

Defined by the Unicode Standard, the name is derived from Unicode (or Universal Coded Character Set) Transformation Format – 8-bit. UTF-8_sentence_1

UTF-8 is capable of encoding all 1,112,064 valid character code points in Unicode using one to four one-byte (8-bit) code units. UTF-8_sentence_2

Code points with lower numerical values, which tend to occur more frequently, are encoded using fewer bytes. UTF-8_sentence_3

It was designed for backward compatibility with ASCII: the first 128 characters of Unicode, which correspond one-to-one with ASCII, are encoded using a single byte with the same binary value as ASCII, so that valid ASCII text is valid UTF-8-encoded Unicode as well. UTF-8_sentence_4

Since ASCII bytes do not occur when encoding non-ASCII code points into UTF-8, UTF-8 is safe to use within most programming and document languages that interpret certain ASCII characters in a special way, such as "/" (slash) in filenames, "\" (backslash) in escape sequences, and "%" in printf. UTF-8_sentence_5

UTF-8 was designed as a superior alternative to UTF-1, a proposed variable-width encoding with partial ASCII compatibility which lacked some features including self-synchronization and fully ASCII-compatible handling of characters such as slashes. UTF-8_sentence_6

Ken Thompson and Rob Pike produced the first implementation for the Plan 9 operating system in September 1992. UTF-8_sentence_7

This led to its adoption by X/Open as its specification for FSS-UTF, which would first be officially presented at USENIX in January 1993 and subsequently adopted by the Internet Engineering Task Force (IETF) in (BCP 18) for future Internet standards work, replacing Single Byte Character Sets such as Latin-1 in older RFCs. UTF-8_sentence_8

UTF-8 is by far the most common encoding for the World Wide Web, accounting for 96% of all web pages, and up to 100% for some languages, as of 2020. UTF-8_sentence_9

Adoption UTF-8_section_0

UTF-8 is the recommendation from the WHATWG for HTML and DOM specifications, and the Internet Mail Consortium recommends that all e-mail programs be able to display and create mail using UTF-8. UTF-8_sentence_10

Google reported that in 2008, UTF-8 (labelled "Unicode") became the most common encoding for HTML files. UTF-8_sentence_11

Since 2009, UTF-8 has been the most common encoding for the World Wide Web. UTF-8_sentence_12

The World Wide Web Consortium recommends UTF-8 as the default encoding in XML and HTML (and not just using UTF-8, also stating it in metadata), "even when all characters are in the ASCII range .. UTF-8_sentence_13

Using non-UTF-8 encodings can have unexpected results". UTF-8_sentence_14

Many other standards only support UTF-8, e.g. open JSON exchange requires it. UTF-8_sentence_15

As of December 2020, UTF-8 accounts on average for 95.9% of all web pages and 96% of the top 1,000 highest ranked web pages. UTF-8_sentence_16

This takes into account that ASCII is valid UTF-8. UTF-8_sentence_17

In locales where UTF-8 is used alongside another encoding, the latter is typically more efficient for the associated language. UTF-8_sentence_18

GB 18030 (effectively) has a 13.3% share in China and a 0.4% share world-wide. UTF-8_sentence_19

Big5 is another popular Chinese encoding with 0.1% share world-wide. UTF-8_sentence_20

The single-byte Windows-1251 is twice as efficient for the Cyrillic script and is used for 10.1% of Russian web sites. UTF-8_sentence_21

E.g. Greek and Hebrew encodings are also twice as efficient, but still those languages have over 95% use of UTF-8. UTF-8_sentence_22

EUC-KR is more efficient for Korean text and is used for 15.1% of South Korean websites. UTF-8_sentence_23

Shift JIS and EUC-JP have a 10.5% share on Japanese websites (the more popular Shift JIS has 0.2% global share). UTF-8_sentence_24

With the exception of GB 18030 and UTF-16, these encodings were designed for specific languages, and do not support all Unicode characters. UTF-8_sentence_25

As of December 2020, the Avestan language has the lowest UTF-8 use on the Web of any tracked language, with 81.4% use. UTF-8_sentence_26

Several languages have 100.0% use of UTF-8 on the web, such as Punjabi, Tagalog, Lao, Marathi, Kannada, Kurdish, Pashto, Javanese, Greenlandic (Kalaallisut) and Iranian languages and sign languages. UTF-8_sentence_27

For local text files UTF-8 usage is lower, and many legacy single-byte encodings remain in use. UTF-8_sentence_28

This is primarily due to editors that will not display or write UTF-8 unless the first character in a file is a byte order mark, making it impossible for other software to use UTF-8 without being rewritten to ignore the byte order mark on input and add it on output. UTF-8_sentence_29

UTF-16 files are also fairly common on Windows, but not elsewhere. UTF-8_sentence_30

Internally in software usage is even lower, with UCS-2 and UTF-32 in use, particularly in Windows but also still to some degree in Python (while not in PyPy), JavaScript, Qt, and many other software libraries. UTF-8_sentence_31

This is due to a belief that direct indexing of code points is more important than 8-bit compatibility. UTF-8_sentence_32

UTF-16 is also used due to being compatible with UCS-2, even though it does not have direct indexing. UTF-8_sentence_33

International Components for Unicode (ICU) has historically used UTF-16, and still does only for Java; while for C/C++ UTF-8 is now supported as the "Default Charset" including the correct handling of "illegal UTF-8". UTF-8_sentence_34

Microsoft now recommends the use of UTF-8 for applications using the Windows API, while continuing to maintain a legacy "Unicode" (meaning UTF-16) interface. UTF-8_sentence_35

Encoding UTF-8_section_1

Since the restriction of the Unicode code-space to 21-bit values in 2003, UTF-8 is defined to encode code points in one to four bytes, depending on the number of significant bits in the numerical value of the code point. UTF-8_sentence_36

The following table shows the structure of the encoding. UTF-8_sentence_37

The x characters are replaced by the bits of the code point. UTF-8_sentence_38

UTF-8_table_general_1

Layout of UTF-8 byte sequencesUTF-8_table_caption_1
Number of bytesUTF-8_header_cell_1_0_0 First code pointUTF-8_header_cell_1_0_1 Last code pointUTF-8_header_cell_1_0_2 Byte 1UTF-8_header_cell_1_0_3 Byte 2UTF-8_header_cell_1_0_4 Byte 3UTF-8_header_cell_1_0_5 Byte 4UTF-8_header_cell_1_0_6
1UTF-8_cell_1_1_0 U+0000UTF-8_cell_1_1_1 U+007FUTF-8_cell_1_1_2 0xxxxxxxUTF-8_cell_1_1_3 UTF-8_cell_1_1_4
2UTF-8_cell_1_2_0 U+0080UTF-8_cell_1_2_1 U+07FFUTF-8_cell_1_2_2 110xxxxxUTF-8_cell_1_2_3 10xxxxxxUTF-8_cell_1_2_4 UTF-8_cell_1_2_5
3UTF-8_cell_1_3_0 U+0800UTF-8_cell_1_3_1 U+FFFFUTF-8_cell_1_3_2 1110xxxxUTF-8_cell_1_3_3 10xxxxxxUTF-8_cell_1_3_4 10xxxxxxUTF-8_cell_1_3_5 UTF-8_cell_1_3_6
4UTF-8_cell_1_4_0 U+10000UTF-8_cell_1_4_1 U+10FFFFUTF-8_cell_1_4_2 11110xxxUTF-8_cell_1_4_3 10xxxxxxUTF-8_cell_1_4_4 10xxxxxxUTF-8_cell_1_4_5 10xxxxxxUTF-8_cell_1_4_6

The first 128 characters (US-ASCII) need one byte. UTF-8_sentence_39

The next 1,920 characters need two bytes to encode, which covers the remainder of almost all Latin-script alphabets, and also Greek, Cyrillic, Coptic, Armenian, Hebrew, Arabic, Syriac, Thaana and N'Ko alphabets, as well as Combining Diacritical Marks. UTF-8_sentence_40

Three bytes are needed for characters in the rest of the Basic Multilingual Plane, which contains virtually all characters in common use, including most Chinese, Japanese and Korean characters. UTF-8_sentence_41

Four bytes are needed for characters in the other planes of Unicode, which include less common CJK characters, various historic scripts, mathematical symbols, and emoji (pictographic symbols). UTF-8_sentence_42

Examples UTF-8_section_2

Consider the encoding of the Euro sign, €: UTF-8_sentence_43

UTF-8_ordered_list_0

  1. The Unicode code point for "€" is U+20AC.UTF-8_item_0_0
  2. As this code point lies between U+0800 and U+FFFF, this will take three bytes to encode.UTF-8_item_0_1
  3. Hexadecimal 20AC is binary 0010 0000 1010 1100. The two leading zeros are added because a three-byte encoding needs exactly sixteen bits from the code point.UTF-8_item_0_2
  4. Because the encoding will be three bytes long, its leading byte starts with three 1s, then a 0 (1110...)UTF-8_item_0_3
  5. The four most significant bits of the code point are stored in the remaining low order four bits of this byte (1110 0010), leaving 12 bits of the code point yet to be encoded (...0000 1010 1100).UTF-8_item_0_4
  6. All continuation bytes contain exactly six bits from the code point. So the next six bits of the code point are stored in the low order six bits of the next byte, and 10 is stored in the high order two bits to mark it as a continuation byte (so 1000 0010).UTF-8_item_0_5
  7. Finally the last six bits of the code point are stored in the low order six bits of the final byte, and again 10 is stored in the high order two bits (1010 1100).UTF-8_item_0_6

The three bytes 1110 0010 1000 0010 1010 1100 can be more concisely written in hexadecimal, as E2 82 AC. UTF-8_sentence_44

The following table summarises this conversion, as well as others with different lengths in UTF-8. UTF-8_sentence_45

The colors indicate how bits from the code point are distributed among the UTF-8 bytes. UTF-8_sentence_46

Additional bits added by the UTF-8 encoding process are shown in black. UTF-8_sentence_47

UTF-8's use of six bits per byte to represent the actual characters being encoded means that octal notation (which uses 3-bit groups) can aid in the comparison of UTF-8 sequences with one another. UTF-8_sentence_48

Codepage layout UTF-8_section_3

The following table summarizes usage of UTF-8 code units (individual bytes or octets) in a code page format. UTF-8_sentence_49

The upper half (0_ to 7_) is for bytes used only in single-byte codes, so it looks like a normal code page; the lower half is for continuation bytes (8_ to B_) and leading bytes (C_ to F_), and is explained further in the legend below. UTF-8_sentence_50

UTF-8_table_general_2

UTF-8UTF-8_table_caption_2
UTF-8_header_cell_2_0_0 _0UTF-8_header_cell_2_0_1 _1UTF-8_header_cell_2_0_2 _2UTF-8_header_cell_2_0_3 _3UTF-8_header_cell_2_0_4 _4UTF-8_header_cell_2_0_5 _5UTF-8_header_cell_2_0_6 _6UTF-8_header_cell_2_0_7 _7UTF-8_header_cell_2_0_8 _8UTF-8_header_cell_2_0_9 _9UTF-8_header_cell_2_0_10 _AUTF-8_header_cell_2_0_11 _BUTF-8_header_cell_2_0_12 _CUTF-8_header_cell_2_0_13 _DUTF-8_header_cell_2_0_14 _EUTF-8_header_cell_2_0_15 _FUTF-8_header_cell_2_0_16
(1 byte)

0_UTF-8_header_cell_2_1_0

NUL

0000UTF-8_cell_2_1_1

SOH

0001UTF-8_cell_2_1_2

STX

0002UTF-8_cell_2_1_3

ETX

0003UTF-8_cell_2_1_4

EOT

0004UTF-8_cell_2_1_5

ENQ

0005UTF-8_cell_2_1_6

ACK

0006UTF-8_cell_2_1_7

BEL

0007UTF-8_cell_2_1_8

BS

0008UTF-8_cell_2_1_9

HT

0009UTF-8_cell_2_1_10

LF

000AUTF-8_cell_2_1_11

VT

000BUTF-8_cell_2_1_12

FF

000CUTF-8_cell_2_1_13

CR

000DUTF-8_cell_2_1_14

SO

000EUTF-8_cell_2_1_15

SI

000FUTF-8_cell_2_1_16

(1)

1_UTF-8_header_cell_2_2_0

DLE

0010UTF-8_cell_2_2_1

DC1

0011UTF-8_cell_2_2_2

DC2

0012UTF-8_cell_2_2_3

DC3

0013UTF-8_cell_2_2_4

DC4

0014UTF-8_cell_2_2_5

NAK

0015UTF-8_cell_2_2_6

SYN

0016UTF-8_cell_2_2_7

ETB

0017UTF-8_cell_2_2_8

CAN

0018UTF-8_cell_2_2_9

EM

0019UTF-8_cell_2_2_10

SUB

001AUTF-8_cell_2_2_11

ESC

001BUTF-8_cell_2_2_12

001CUTF-8_cell_2_2_13 GS

001DUTF-8_cell_2_2_14

RS

001EUTF-8_cell_2_2_15

US

001FUTF-8_cell_2_2_16

(1)

2_UTF-8_header_cell_2_3_0

SP

0020UTF-8_cell_2_3_1

!

0021UTF-8_cell_2_3_2

"

0022UTF-8_cell_2_3_3

#

0023UTF-8_cell_2_3_4

$

0024UTF-8_cell_2_3_5

%

0025UTF-8_cell_2_3_6

&

0026UTF-8_cell_2_3_7

'

0027UTF-8_cell_2_3_8

(

0028UTF-8_cell_2_3_9

)

0029UTF-8_cell_2_3_10

*

002AUTF-8_cell_2_3_11

+

002BUTF-8_cell_2_3_12

,

002CUTF-8_cell_2_3_13

-

002DUTF-8_cell_2_3_14

.

002EUTF-8_cell_2_3_15

/

002FUTF-8_cell_2_3_16

(1)

3_UTF-8_header_cell_2_4_0

0

0030UTF-8_cell_2_4_1

1

0031UTF-8_cell_2_4_2

2

0032UTF-8_cell_2_4_3

3

0033UTF-8_cell_2_4_4

4

0034UTF-8_cell_2_4_5

5

0035UTF-8_cell_2_4_6

6

0036UTF-8_cell_2_4_7

7

0037UTF-8_cell_2_4_8

8

0038UTF-8_cell_2_4_9

9

0039UTF-8_cell_2_4_10

:

003AUTF-8_cell_2_4_11

;

003BUTF-8_cell_2_4_12

<

003CUTF-8_cell_2_4_13

=

003DUTF-8_cell_2_4_14

>

003EUTF-8_cell_2_4_15

?

003FUTF-8_cell_2_4_16

(1)

4_UTF-8_header_cell_2_5_0

@

0040UTF-8_cell_2_5_1

A

0041UTF-8_cell_2_5_2

B

0042UTF-8_cell_2_5_3

C

0043UTF-8_cell_2_5_4

D

0044UTF-8_cell_2_5_5

E

0045UTF-8_cell_2_5_6

F

0046UTF-8_cell_2_5_7

G

0047UTF-8_cell_2_5_8

H

0048UTF-8_cell_2_5_9

I

0049UTF-8_cell_2_5_10

J

004AUTF-8_cell_2_5_11

K

004BUTF-8_cell_2_5_12

L

004CUTF-8_cell_2_5_13

M

004DUTF-8_cell_2_5_14

N

004EUTF-8_cell_2_5_15

O

004FUTF-8_cell_2_5_16

(1)

5_UTF-8_header_cell_2_6_0

P

0050UTF-8_cell_2_6_1

Q

0051UTF-8_cell_2_6_2

R

0052UTF-8_cell_2_6_3

S

0053UTF-8_cell_2_6_4

T

0054UTF-8_cell_2_6_5

U

0055UTF-8_cell_2_6_6

V

0056UTF-8_cell_2_6_7

W

0057UTF-8_cell_2_6_8

X

0058UTF-8_cell_2_6_9

Y

0059UTF-8_cell_2_6_10

Z

005AUTF-8_cell_2_6_11

[

005BUTF-8_cell_2_6_12

\

005CUTF-8_cell_2_6_13

Square_brackets

005DUTF-8_cell_2_6_14

^

005EUTF-8_cell_2_6_15

_

005FUTF-8_cell_2_6_16

(1)

6_UTF-8_header_cell_2_7_0

`

0060UTF-8_cell_2_7_1

a

0061UTF-8_cell_2_7_2

b

0062UTF-8_cell_2_7_3

c

0063UTF-8_cell_2_7_4

d

0064UTF-8_cell_2_7_5

e

0065UTF-8_cell_2_7_6

f

0066UTF-8_cell_2_7_7

g

0067UTF-8_cell_2_7_8

h

0068UTF-8_cell_2_7_9

i

0069UTF-8_cell_2_7_10

j

006AUTF-8_cell_2_7_11

k

006BUTF-8_cell_2_7_12

l

006CUTF-8_cell_2_7_13

m

006DUTF-8_cell_2_7_14

n

006EUTF-8_cell_2_7_15

o

006FUTF-8_cell_2_7_16

(1)

7_UTF-8_header_cell_2_8_0

p

0070UTF-8_cell_2_8_1

q

0071UTF-8_cell_2_8_2

r

0072UTF-8_cell_2_8_3

s

0073UTF-8_cell_2_8_4

t

0074UTF-8_cell_2_8_5

u

0075UTF-8_cell_2_8_6

v

0076UTF-8_cell_2_8_7

w

0077UTF-8_cell_2_8_8

x

0078UTF-8_cell_2_8_9

y

0079UTF-8_cell_2_8_10

z

007AUTF-8_cell_2_8_11

{

007BUTF-8_cell_2_8_12

[[Vertical_bar|]]

007CUTF-8_cell_2_8_13

}

007DUTF-8_cell_2_8_14

~

007EUTF-8_cell_2_8_15

DEL

007FUTF-8_cell_2_8_16

8_UTF-8_header_cell_2_9_0 +00UTF-8_cell_2_9_1 +01UTF-8_cell_2_9_2 +02UTF-8_cell_2_9_3 +03UTF-8_cell_2_9_4 +04UTF-8_cell_2_9_5 +05UTF-8_cell_2_9_6 +06UTF-8_cell_2_9_7 +07UTF-8_cell_2_9_8 +08UTF-8_cell_2_9_9 +09UTF-8_cell_2_9_10 +0AUTF-8_cell_2_9_11 +0BUTF-8_cell_2_9_12 +0CUTF-8_cell_2_9_13 +0DUTF-8_cell_2_9_14 +0EUTF-8_cell_2_9_15 +0FUTF-8_cell_2_9_16
9_UTF-8_header_cell_2_10_0 +10UTF-8_cell_2_10_1 +11UTF-8_cell_2_10_2 +12UTF-8_cell_2_10_3 +13UTF-8_cell_2_10_4 +14UTF-8_cell_2_10_5 +15UTF-8_cell_2_10_6 +16UTF-8_cell_2_10_7 +17UTF-8_cell_2_10_8 +18UTF-8_cell_2_10_9 +19UTF-8_cell_2_10_10 +1AUTF-8_cell_2_10_11 +1BUTF-8_cell_2_10_12 +1CUTF-8_cell_2_10_13 +1DUTF-8_cell_2_10_14 +1EUTF-8_cell_2_10_15 +1FUTF-8_cell_2_10_16
A_UTF-8_header_cell_2_11_0 +20UTF-8_cell_2_11_1 +21UTF-8_cell_2_11_2 +22UTF-8_cell_2_11_3 +23UTF-8_cell_2_11_4 +24UTF-8_cell_2_11_5 +25UTF-8_cell_2_11_6 +26UTF-8_cell_2_11_7 +27UTF-8_cell_2_11_8 +28UTF-8_cell_2_11_9 +29UTF-8_cell_2_11_10 +2AUTF-8_cell_2_11_11 +2BUTF-8_cell_2_11_12 +2CUTF-8_cell_2_11_13 +2DUTF-8_cell_2_11_14 +2EUTF-8_cell_2_11_15 +2FUTF-8_cell_2_11_16
B_UTF-8_header_cell_2_12_0 +30UTF-8_cell_2_12_1 +31UTF-8_cell_2_12_2 +32UTF-8_cell_2_12_3 +33UTF-8_cell_2_12_4 +34UTF-8_cell_2_12_5 +35UTF-8_cell_2_12_6 +36UTF-8_cell_2_12_7 +37UTF-8_cell_2_12_8 +38UTF-8_cell_2_12_9 +39UTF-8_cell_2_12_10 +3AUTF-8_cell_2_12_11 +3BUTF-8_cell_2_12_12 +3CUTF-8_cell_2_12_13 +3DUTF-8_cell_2_12_14 +3EUTF-8_cell_2_12_15 +3FUTF-8_cell_2_12_16
(2)

C_UTF-8_header_cell_2_13_0

2

0000UTF-8_cell_2_13_1

2

0040UTF-8_cell_2_13_2

Latin

0080UTF-8_cell_2_13_3

Latin

00C0UTF-8_cell_2_13_4

Latin

0100UTF-8_cell_2_13_5

Latin

0140UTF-8_cell_2_13_6

Latin

0180UTF-8_cell_2_13_7

Latin

01C0UTF-8_cell_2_13_8

Latin

0200UTF-8_cell_2_13_9

IPA

0240UTF-8_cell_2_13_10

IPA

0280UTF-8_cell_2_13_11

IPA

02C0UTF-8_cell_2_13_12

accents

0300UTF-8_cell_2_13_13

accents

0340UTF-8_cell_2_13_14

Greek

0380UTF-8_cell_2_13_15

Greek

03C0UTF-8_cell_2_13_16

(2)

D_UTF-8_header_cell_2_14_0

Cyril

0400UTF-8_cell_2_14_1

Cyril

0440UTF-8_cell_2_14_2

Cyril

0480UTF-8_cell_2_14_3

Cyril

04C0UTF-8_cell_2_14_4

Cyril

0500UTF-8_cell_2_14_5

Armeni

0540UTF-8_cell_2_14_6

Hebrew

0580UTF-8_cell_2_14_7

Hebrew

05C0UTF-8_cell_2_14_8

Arabic

0600UTF-8_cell_2_14_9

Arabic

0640UTF-8_cell_2_14_10

Arabic

0680UTF-8_cell_2_14_11

Arabic

06C0UTF-8_cell_2_14_12

Syriac

0700UTF-8_cell_2_14_13

Arabic

0740UTF-8_cell_2_14_14

Thaana

0780UTF-8_cell_2_14_15

N'Ko

07C0UTF-8_cell_2_14_16

(3)

E_UTF-8_header_cell_2_15_0

Indic

0800UTF-8_cell_2_15_1

Misc.

1000UTF-8_cell_2_15_2

Symbol

2000UTF-8_cell_2_15_3

Kana

3000UTF-8_cell_2_15_4

CJK

4000UTF-8_cell_2_15_5

CJK

5000UTF-8_cell_2_15_6

CJK

6000UTF-8_cell_2_15_7

CJK

7000UTF-8_cell_2_15_8

CJK

8000UTF-8_cell_2_15_9

CJK

9000UTF-8_cell_2_15_10

Asian

A000UTF-8_cell_2_15_11

Hangul

B000UTF-8_cell_2_15_12

Hangul

C000UTF-8_cell_2_15_13

Hangul

D000UTF-8_cell_2_15_14

PUA

E000UTF-8_cell_2_15_15

Forms

F000UTF-8_cell_2_15_16

(4)

F_UTF-8_header_cell_2_16_0

SMP…

10000UTF-8_cell_2_16_1

񀀀

40000UTF-8_cell_2_16_2

򀀀

80000UTF-8_cell_2_16_3

SSP…

C0000UTF-8_cell_2_16_4

SPU…

100000UTF-8_cell_2_16_5

4

140000UTF-8_cell_2_16_6

4

180000UTF-8_cell_2_16_7

4

1C0000UTF-8_cell_2_16_8

5

200000UTF-8_cell_2_16_9

5

1000000UTF-8_cell_2_16_10

5

2000000UTF-8_cell_2_16_11

5

3000000UTF-8_cell_2_16_12

6

4000000UTF-8_cell_2_16_13

6

40000000UTF-8_cell_2_16_14

UTF-8_cell_2_16_15 UTF-8_cell_2_16_16

Blue cells are 7-bit (single-byte) sequences. UTF-8_sentence_51

They must not be followed by a continuation byte. UTF-8_sentence_52

Orange cells with a large dot are a continuation byte. UTF-8_sentence_53

The hexadecimal number shown after the + symbol is the value of the 6 bits they add. UTF-8_sentence_54

This character never occurs as the first byte of a multi-byte sequence. UTF-8_sentence_55

White cells are the leading bytes for a sequence of multiple bytes, the length shown at the left edge of the row. UTF-8_sentence_56

The text shows the Unicode blocks encoded by sequences starting with this byte, and the hexadecimal code point shown in the cell is the lowest character value encoded using that leading byte. UTF-8_sentence_57

Red cells must never appear in a valid UTF-8 sequence. UTF-8_sentence_58

The first two red cells (C0 and C1) could be used only for a 2-byte encoding of a 7-bit ASCII character which should be encoded in 1 byte; as described below, such "overlong" sequences are disallowed. UTF-8_sentence_59

To understand why this is, consider the character 128, hex 80, binary 1000 0000. UTF-8_sentence_60

To encode it as 2 characters, the low six bits are stored in the second character as 128 itself 10 000000, but the upper two bits are stored in the first character as 110 00010, making the minimum first character C2. UTF-8_sentence_61

The red cells in the F_ row (F5 to FD) indicate leading bytes of 4-byte or longer sequences that cannot be valid because they would encode code points larger than the U+10FFFF limit of Unicode (a limit derived from the maximum code point encodable in UTF-16 ). UTF-8_sentence_62

FE and FF do not match any allowed character pattern and are therefore not valid start bytes. UTF-8_sentence_63

Pink cells are the leading bytes for a sequence of multiple bytes, of which some, but not all, possible continuation sequences are valid. UTF-8_sentence_64

E0 and F0 could start overlong encodings, in this case the lowest non-overlong-encoded code point is shown. UTF-8_sentence_65

F4 can start code points greater than U+10FFFF which are invalid. UTF-8_sentence_66

ED can start the encoding of a code point in the range U+D800–U+DFFF; these are invalid since they are reserved for UTF-16 surrogate halves. UTF-8_sentence_67

Overlong encodings UTF-8_section_4

In principle, it would be possible to inflate the number of bytes in an encoding by padding the code point with leading 0s. UTF-8_sentence_68

To encode the Euro sign € from the above example in four bytes instead of three, it could be padded with leading 0s until it was 21 bits long – 000 000010 000010 101100, and encoded as 11110000 10000010 10000010 10101100 (or F0 82 82 AC in hexadecimal). UTF-8_sentence_69

This is called an overlong encoding. UTF-8_sentence_70

The standard specifies that the correct encoding of a code point uses only the minimum number of bytes required to hold the significant bits of the code point. UTF-8_sentence_71

Longer encodings are called overlong and are not valid UTF-8 representations of the code point. UTF-8_sentence_72

This rule maintains a one-to-one correspondence between code points and their valid encodings, so that there is a unique valid encoding for each code point. UTF-8_sentence_73

This ensures that string comparisons and searches are well-defined. UTF-8_sentence_74

Invalid sequences and error handling UTF-8_section_5

Not all sequences of bytes are valid UTF-8. UTF-8_sentence_75

A UTF-8 decoder should be prepared for: UTF-8_sentence_76

UTF-8_unordered_list_1

  • invalid bytesUTF-8_item_1_7
  • an unexpected continuation byteUTF-8_item_1_8
  • a non-continuation byte before the end of the characterUTF-8_item_1_9
  • the string ending before the end of the character (which can happen in simple string truncation)UTF-8_item_1_10
  • an overlong encodingUTF-8_item_1_11
  • a sequence that decodes to an invalid code pointUTF-8_item_1_12

Many of the first UTF-8 decoders would decode these, ignoring incorrect bits and accepting overlong results. UTF-8_sentence_77

Carefully crafted invalid UTF-8 could make them either skip or create ASCII characters such as NUL, slash, or quotes. UTF-8_sentence_78

Invalid UTF-8 has been used to bypass security validations in high-profile products including Microsoft's IIS web server and Apache's Tomcat servlet container. UTF-8_sentence_79

states "Implementations of the decoding algorithm MUST protect against decoding invalid sequences." UTF-8_sentence_80

The Unicode Standard requires decoders to "...treat any ill-formed code unit sequence as an error condition. UTF-8_sentence_81

This guarantees that it will neither interpret nor emit an ill-formed code unit sequence." UTF-8_sentence_82

Since (November 2003), the high and low surrogate halves used by UTF-16 (U+D800 through U+DFFF) and code points not encodable by UTF-16 (those after U+10FFFF) are not legal Unicode values, and their UTF-8 encoding must be treated as an invalid byte sequence. UTF-8_sentence_83

Not decoding unpaired surrogate halves makes it impossible to store invalid UTF-16 (such as Windows filenames or UTF-16 that has been split between the surrogates) as UTF-8. UTF-8_sentence_84

Some implementations of decoders throw exceptions on errors. UTF-8_sentence_85

This has the disadvantage that it can turn what would otherwise be harmless errors (such as a "no such file" error) into a denial of service. UTF-8_sentence_86

For instance early versions of Python 3.0 would exit immediately if the command line or environment variables contained invalid UTF-8. UTF-8_sentence_87

An alternative practice is to replace errors with a replacement character. UTF-8_sentence_88

Since Unicode 6 (October 2010), the standard (chapter 3) has recommended a "best practice" where the error ends as soon as a disallowed byte is encountered. UTF-8_sentence_89

In these decoders E1,A0,C0 is two errors (2 bytes in the first one). UTF-8_sentence_90

This means an error is no more than three bytes long and never contains the start of a valid character, and there are 21,952 different possible errors. UTF-8_sentence_91

The standard also recommends replacing each error with the replacement character "�" (U+FFFD). UTF-8_sentence_92

Byte order mark UTF-8_section_6

If the UTF-16 Unicode byte order mark (BOM) character is at the start of a UTF-8 file, the first three bytes will be 0xEF, 0xBB, 0xBF. UTF-8_sentence_93

The Unicode Standard neither requires nor recommends the use of the BOM for UTF-8, but warns that it may be encountered at the start of a file trans-coded from another encoding. UTF-8_sentence_94

While ASCII text encoded using UTF-8 is backward compatible with ASCII, this is not true when Unicode Standard recommendations are ignored and a BOM is added. UTF-8_sentence_95

Nevertheless, there was and still is software that always inserts a BOM when writing UTF-8, and refuses to correctly interpret UTF-8 unless the first character is a BOM (or the file only contains ASCII). UTF-8_sentence_96

Naming UTF-8_section_7

The official Internet Assigned Numbers Authority (IANA) code for the encoding is "UTF-8". UTF-8_sentence_97

All letters are upper-case, and the name is hyphenated. UTF-8_sentence_98

This spelling is used in all the Unicode Consortium documents relating to the encoding. UTF-8_sentence_99

Alternatively, the name "utf-8" may be used by all standards conforming to the IANA list (which include CSS, HTML, XML, and HTTP headers), as the declaration is case insensitive. UTF-8_sentence_100

Other descriptions, such as those that omit the hyphen or replace it with a space, i.e. "utf8" or "UTF 8", are not accepted as correct by the governing standards. UTF-8_sentence_101

Despite this, most agents such as browsers can understand them, and so standards intended to describe existing practice (such as HTML5) may effectively require their recognition. UTF-8_sentence_102

Unofficially, UTF-8-BOM and UTF-8-NOBOM are sometimes used to refer to text files which respectively contain and lack a byte order mark (BOM). UTF-8_sentence_103

In Japan especially, UTF-8 encoding without BOM is sometimes called "UTF-8N". UTF-8_sentence_104

Windows 7 and later, i.e. all supported Windows versions, have codepage 65001, as a synonym for UTF-8 (with better support than in older Windows), and Microsoft has a script for Windows 10, to enable it by default for its program Microsoft Notepad. UTF-8_sentence_105

In PCL, UTF-8 is called Symbol-ID "18N" (PCL supports 183 character encodings, called Symbol Sets, which potentially could be reduced to one, 18N, that is UTF-8). UTF-8_sentence_106

History UTF-8_section_8

See also: Universal Coded Character Set § History UTF-8_sentence_107

The International Organization for Standardization (ISO) set out to compose a universal multi-byte character set in 1989. UTF-8_sentence_108

The draft ISO 10646 standard contained a non-required annex called UTF-1 that provided a byte stream encoding of its 32-bit code points. UTF-8_sentence_109

This encoding was not satisfactory on performance grounds, among other problems, and the biggest problem was probably that it did not have a clear separation between ASCII and non-ASCII: new UTF-1 tools would be backward compatible with ASCII-encoded text, but UTF-1-encoded text could confuse existing code expecting ASCII (or extended ASCII), because it could contain continuation bytes in the range 0x21–0x7E that meant something else in ASCII, e.g., 0x2F for '/', the Unix path directory separator, and this example is reflected in the name and introductory text of its replacement. UTF-8_sentence_110

The table below was derived from a textual description in the annex. UTF-8_sentence_111

UTF-8_table_general_3

UTF-1UTF-8_table_caption_3
Number

of bytesUTF-8_header_cell_3_0_0

First

code pointUTF-8_header_cell_3_0_1

Last

code pointUTF-8_header_cell_3_0_2

Byte 1UTF-8_header_cell_3_0_3 Byte 2UTF-8_header_cell_3_0_4 Byte 3UTF-8_header_cell_3_0_5 Byte 4UTF-8_header_cell_3_0_6 Byte 5UTF-8_header_cell_3_0_7
1UTF-8_cell_3_1_0 U+0000UTF-8_cell_3_1_1 U+009FUTF-8_cell_3_1_2 00–9FUTF-8_cell_3_1_3 UTF-8_cell_3_1_4 UTF-8_cell_3_1_5 UTF-8_cell_3_1_6 UTF-8_cell_3_1_7
2UTF-8_cell_3_2_0 U+00A0UTF-8_cell_3_2_1 U+00FFUTF-8_cell_3_2_2 A0UTF-8_cell_3_2_3 A0–FFUTF-8_cell_3_2_4 UTF-8_cell_3_2_5 UTF-8_cell_3_2_6 UTF-8_cell_3_2_7
2UTF-8_cell_3_3_0 U+0100UTF-8_cell_3_3_1 U+4015UTF-8_cell_3_3_2 A1–F5UTF-8_cell_3_3_3 21–7E, A0–FFUTF-8_cell_3_3_4 UTF-8_cell_3_3_5 UTF-8_cell_3_3_6 UTF-8_cell_3_3_7
3UTF-8_cell_3_4_0 U+4016UTF-8_cell_3_4_1 U+38E2DUTF-8_cell_3_4_2 F6–FBUTF-8_cell_3_4_3 21–7E, A0–FFUTF-8_cell_3_4_4 21–7E, A0–FFUTF-8_cell_3_4_5 UTF-8_cell_3_4_6 UTF-8_cell_3_4_7
5UTF-8_cell_3_5_0 U+38E2EUTF-8_cell_3_5_1 U+7FFFFFFFUTF-8_cell_3_5_2 FC–FFUTF-8_cell_3_5_3 21–7E, A0–FFUTF-8_cell_3_5_4 21–7E, A0–FFUTF-8_cell_3_5_5 21–7E, A0–FFUTF-8_cell_3_5_6 21–7E, A0–FFUTF-8_cell_3_5_7

In July 1992, the X/Open committee XoJIG was looking for a better encoding. UTF-8_sentence_112

Dave Prosser of Unix System Laboratories submitted a proposal for one that had faster implementation characteristics and introduced the improvement that 7-bit ASCII characters would only represent themselves; all multi-byte sequences would include only bytes where the high bit was set. UTF-8_sentence_113

The name File System Safe UCS Transformation Format (FSS-UTF) and most of the text of this proposal were later preserved in the final specification. UTF-8_sentence_114

FSS-UTF UTF-8_section_9

UTF-8_table_general_4

FSS-UTF proposal (1992)UTF-8_table_caption_4
Number

of bytesUTF-8_header_cell_4_0_0

First

code pointUTF-8_header_cell_4_0_1

Last

code pointUTF-8_header_cell_4_0_2

Byte 1UTF-8_header_cell_4_0_3 Byte 2UTF-8_header_cell_4_0_4 Byte 3UTF-8_header_cell_4_0_5 Byte 4UTF-8_header_cell_4_0_6 Byte 5UTF-8_header_cell_4_0_7
1UTF-8_cell_4_1_0 U+0000UTF-8_cell_4_1_1 U+007FUTF-8_cell_4_1_2 0xxxxxxxUTF-8_cell_4_1_3 UTF-8_cell_4_1_4 UTF-8_cell_4_1_5 UTF-8_cell_4_1_6 UTF-8_cell_4_1_7
2UTF-8_cell_4_2_0 U+0080UTF-8_cell_4_2_1 U+207FUTF-8_cell_4_2_2 10xxxxxxUTF-8_cell_4_2_3 1xxxxxxxUTF-8_cell_4_2_4 UTF-8_cell_4_2_5 UTF-8_cell_4_2_6 UTF-8_cell_4_2_7
3UTF-8_cell_4_3_0 U+2080UTF-8_cell_4_3_1 U+8207FUTF-8_cell_4_3_2 110xxxxxUTF-8_cell_4_3_3 1xxxxxxxUTF-8_cell_4_3_4 1xxxxxxxUTF-8_cell_4_3_5 UTF-8_cell_4_3_6 UTF-8_cell_4_3_7
4UTF-8_cell_4_4_0 U+82080UTF-8_cell_4_4_1 U+208207FUTF-8_cell_4_4_2 1110xxxxUTF-8_cell_4_4_3 1xxxxxxxUTF-8_cell_4_4_4 1xxxxxxxUTF-8_cell_4_4_5 1xxxxxxxUTF-8_cell_4_4_6 UTF-8_cell_4_4_7
5UTF-8_cell_4_5_0 U+2082080UTF-8_cell_4_5_1 U+7FFFFFFFUTF-8_cell_4_5_2 11110xxxUTF-8_cell_4_5_3 1xxxxxxxUTF-8_cell_4_5_4 1xxxxxxxUTF-8_cell_4_5_5 1xxxxxxxUTF-8_cell_4_5_6 1xxxxxxxUTF-8_cell_4_5_7

In August 1992, this proposal was circulated by an IBM X/Open representative to interested parties. UTF-8_sentence_115

A modification by Ken Thompson of the Plan 9 operating system group at Bell Labs made it somewhat less bit-efficient than the previous proposal but crucially allowed it to be self-synchronizing, letting a reader start anywhere and immediately detect byte sequence boundaries. UTF-8_sentence_116

It also abandoned the use of biases and instead added the rule that only the shortest possible encoding is allowed; the additional loss in compactness is relatively insignificant, but readers now have to look out for invalid encodings to avoid reliability and especially security issues. UTF-8_sentence_117

Thompson's design was outlined on September 2, 1992, on a placemat in a New Jersey diner with Rob Pike. UTF-8_sentence_118

In the following days, Pike and Thompson implemented it and updated Plan 9 to use it throughout, and then communicated their success back to X/Open, which accepted it as the specification for FSS-UTF. UTF-8_sentence_119

UTF-8_table_general_5

FSS-UTF (1992) / UTF-8 (1993)UTF-8_table_caption_5
Number

of bytesUTF-8_header_cell_5_0_0

First

code pointUTF-8_header_cell_5_0_1

Last

code pointUTF-8_header_cell_5_0_2

Byte 1UTF-8_header_cell_5_0_3 Byte 2UTF-8_header_cell_5_0_4 Byte 3UTF-8_header_cell_5_0_5 Byte 4UTF-8_header_cell_5_0_6 Byte 5UTF-8_header_cell_5_0_7 Byte 6UTF-8_header_cell_5_0_8
1UTF-8_cell_5_1_0 U+0000UTF-8_cell_5_1_1 U+007FUTF-8_cell_5_1_2 0xxxxxxxUTF-8_cell_5_1_3 UTF-8_cell_5_1_4 UTF-8_cell_5_1_5 UTF-8_cell_5_1_6 UTF-8_cell_5_1_7 UTF-8_cell_5_1_8
2UTF-8_cell_5_2_0 U+0080UTF-8_cell_5_2_1 U+07FFUTF-8_cell_5_2_2 110xxxxxUTF-8_cell_5_2_3 10xxxxxxUTF-8_cell_5_2_4 UTF-8_cell_5_2_5 UTF-8_cell_5_2_6 UTF-8_cell_5_2_7 UTF-8_cell_5_2_8
3UTF-8_cell_5_3_0 U+0800UTF-8_cell_5_3_1 U+FFFFUTF-8_cell_5_3_2 1110xxxxUTF-8_cell_5_3_3 10xxxxxxUTF-8_cell_5_3_4 10xxxxxxUTF-8_cell_5_3_5 UTF-8_cell_5_3_6 UTF-8_cell_5_3_7 UTF-8_cell_5_3_8
4UTF-8_cell_5_4_0 U+10000UTF-8_cell_5_4_1 U+1FFFFFUTF-8_cell_5_4_2 11110xxxUTF-8_cell_5_4_3 10xxxxxxUTF-8_cell_5_4_4 10xxxxxxUTF-8_cell_5_4_5 10xxxxxxUTF-8_cell_5_4_6 UTF-8_cell_5_4_7 UTF-8_cell_5_4_8
5UTF-8_cell_5_5_0 U+200000UTF-8_cell_5_5_1 U+3FFFFFFUTF-8_cell_5_5_2 111110xxUTF-8_cell_5_5_3 10xxxxxxUTF-8_cell_5_5_4 10xxxxxxUTF-8_cell_5_5_5 10xxxxxxUTF-8_cell_5_5_6 10xxxxxxUTF-8_cell_5_5_7 UTF-8_cell_5_5_8
6UTF-8_cell_5_6_0 U+4000000UTF-8_cell_5_6_1 U+7FFFFFFFUTF-8_cell_5_6_2 1111110xUTF-8_cell_5_6_3 10xxxxxxUTF-8_cell_5_6_4 10xxxxxxUTF-8_cell_5_6_5 10xxxxxxUTF-8_cell_5_6_6 10xxxxxxUTF-8_cell_5_6_7 10xxxxxxUTF-8_cell_5_6_8

UTF-8 was first officially presented at the USENIX conference in San Diego, from January 25 to 29, 1993. UTF-8_sentence_120

The Internet Engineering Task Force adopted UTF-8 in its Policy on Character Sets and Languages in (BCP 18) for future Internet standards work, replacing Single Byte Character Sets such as Latin-1 in older RFCs. UTF-8_sentence_121

In November 2003, UTF-8 was restricted by to match the constraints of the UTF-16 character encoding: explicitly prohibiting code points corresponding to the high and low surrogate characters removed more than 3% of the three-byte sequences, and ending at U+10FFFF removed more than 48% of the four-byte sequences and all five- and six-byte sequences. UTF-8_sentence_122

Standards UTF-8_section_10

There are several current definitions of UTF-8 in various standards documents: UTF-8_sentence_123

UTF-8_unordered_list_2

  • / STD 63 (2003), which establishes UTF-8 as a standard Internet protocol elementUTF-8_item_2_13
  • defines UTF-8 NFC for Network Interchange (2008)UTF-8_item_2_14
  • ISO/IEC 10646:2014 §9.1 (2014)UTF-8_item_2_15
  • The Unicode Standard, Version 11.0 (2018)UTF-8_item_2_16

They supersede the definitions given in the following obsolete works: UTF-8_sentence_124

UTF-8_unordered_list_3

  • The Unicode Standard, Version 2.0, Appendix A (1996)UTF-8_item_3_17
  • ISO/IEC 10646-1:1993 Amendment 2 / Annex R (1996)UTF-8_item_3_18
  • (1996)UTF-8_item_3_19
  • (1998)UTF-8_item_3_20
  • The Unicode Standard, Version 3.0, §2.3 (2000) plus Corrigendum #1 : UTF-8 Shortest Form (2000)UTF-8_item_3_21
  • Unicode Standard Annex #27: Unicode 3.1 (2001)UTF-8_item_3_22
  • The Unicode Standard, Version 5.0 (2006)UTF-8_item_3_23
  • The Unicode Standard, Version 6.0 (2010)UTF-8_item_3_24

They are all the same in their general mechanics, with the main differences being on issues such as allowed range of code point values and safe handling of invalid input. UTF-8_sentence_125

Comparison with other encodings UTF-8_section_11

See also: Comparison of Unicode encodings UTF-8_sentence_126

Some of the important features of this encoding are as follows: UTF-8_sentence_127

UTF-8_unordered_list_4

  • Backward compatibility: Backward compatibility with ASCII and the enormous amount of software designed to process ASCII-encoded text was the main driving force behind the design of UTF-8. In UTF-8, single bytes with values in the range of 0 to 127 map directly to Unicode code points in the ASCII range. Single bytes in this range represent characters, as they do in ASCII. Moreover, 7-bit bytes (bytes where the most significant bit is 0) never appear in a multi-byte sequence, and no valid multi-byte sequence decodes to an ASCII code-point. A sequence of 7-bit bytes is both valid ASCII and valid UTF-8, and under either interpretation represents the same sequence of characters. Therefore, the 7-bit bytes in a UTF-8 stream represent all and only the ASCII characters in the stream. Thus, many text processors, parsers, protocols, file formats, text display programs, etc., which use ASCII characters for formatting and control purposes, will continue to work as intended by treating the UTF-8 byte stream as a sequence of single-byte characters, without decoding the multi-byte sequences. ASCII characters on which the processing turns, such as punctuation, whitespace, and control characters will never be encoded as multi-byte sequences. It is therefore safe for such processors to simply ignore or pass-through the multi-byte sequences, without decoding them. For example, ASCII whitespace may be used to tokenize a UTF-8 stream into words; ASCII line-feeds may be used to split a UTF-8 stream into lines; and ASCII NUL characters can be used to split UTF-8-encoded data into null-terminated strings. Similarly, many format strings used by library functions like "printf" will correctly handle UTF-8-encoded input arguments.UTF-8_item_4_25
  • Fallback and auto-detection: Only a small subset of possible byte strings are a valid UTF-8 string: the bytes C0, C1, and F5 through FF cannot appear, and bytes with the high bit set must be in pairs, and other requirements. It is extremely unlikely that a readable text in any extended ASCII is valid UTF-8. Part of the popularity of UTF-8 is due to it providing a form of backward compatibility for these as well. A UTF-8 processor which erroneously receives extended ASCII as input can thus "auto-detect" this with very high reliability. Fallback errors will be false negatives, and these will be rare. Moreover, in many applications, such as text display, the consequence of incorrect fallback is usually slight. A UTF-8 stream may simply contain errors, resulting in the auto-detection scheme producing false positives; but auto-detection is successful in the majority of cases, especially with longer texts, and is widely used. It also works to "fall back" or replace 8-bit bytes using the appropriate code-point for a legacy encoding only when errors in the UTF-8 are detected, allowing recovery even if UTF-8 and legacy encoding is concatenated in the same file.UTF-8_item_4_26
  • Prefix code: The first byte indicates the number of bytes in the sequence. Reading from a stream can instantaneously decode each individual fully received sequence, without first having to wait for either the first byte of a next sequence or an end-of-stream indication. The length of multi-byte sequences is easily determined by humans as it is simply the number of high-order 1s in the leading byte. An incorrect character will not be decoded if a stream ends mid-sequence.UTF-8_item_4_27
  • Self-synchronization: The leading bytes and the continuation bytes do not share values (continuation bytes start with the bits 10 while single bytes start with 0 and longer lead bytes start with 11). This means a search will not accidentally find the sequence for one character starting in the middle of another character. It also means the start of a character can be found from a random position by backing up at most 3 bytes to find the leading byte. An incorrect character will not be decoded if a stream starts mid-sequence, and a shorter sequence will never appear inside a longer one.UTF-8_item_4_28
  • Sorting order: The chosen values of the leading bytes means that a list of UTF-8 strings can be sorted in code point order by sorting the corresponding byte sequences.UTF-8_item_4_29

Single-byte UTF-8_section_12

UTF-8_unordered_list_5

  • UTF-8 can encode any Unicode character, avoiding the need to figure out and set a "code page" or otherwise indicate what character set is in use, and allowing output in multiple scripts at the same time. For many scripts there have been more than one single-byte encoding in usage, so even knowing the script was insufficient information to display it correctly.UTF-8_item_5_30
  • The bytes 0xFE and 0xFF do not appear, so a valid UTF-8 stream never matches the UTF-16 byte order mark and thus cannot be confused with it. The absence of 0xFF (0377) also eliminates the need to escape this byte in Telnet (and FTP control connection).UTF-8_item_5_31
  • UTF-8 encoded text is larger than specialized single-byte encodings except for plain ASCII characters. In the case of scripts which used 8-bit character sets with non-Latin characters encoded in the upper half (such as most Cyrillic and Greek alphabet code pages), characters in UTF-8 will be double the size. For some scripts, such as Thai and Devanagari (which is used by various South Asian languages), characters will triple in size. There are even examples where a single byte turns into a composite character in Unicode and is thus six times larger in UTF-8. This has caused objections in India and other countries.UTF-8_item_5_32
  • It is possible in UTF-8 (or any other variable-length encoding) to split or truncate a string in the middle of a character. If the two pieces are not re-appended later before interpretation as characters, this can introduce an invalid sequence at both the end of the previous section and the start of the next, and some decoders will not preserve these bytes and result in data loss. Because UTF-8 is self-synchronizing this will however never introduce a different valid character, and it is also fairly easy to move the truncation point backward to the start of a character.UTF-8_item_5_33
  • If the code points are all the same size, measurements of a fixed number of them is easy. Due to ASCII-era documentation where "character" is used as a synonym for "byte" this is often considered important. However, by measuring string positions using bytes instead of "characters" most algorithms can be easily and efficiently adapted for UTF-8. Searching for a string within a long string can for example be done byte by byte; the self-synchronization property prevents false positives.UTF-8_item_5_34

Other multi-byte UTF-8_section_13

UTF-8_unordered_list_6

  • UTF-8 can encode any Unicode character. Files in different scripts can be displayed correctly without having to choose the correct code page or font. For instance, Chinese and Arabic can be written in the same file without specialised markup or manual settings that specify an encoding.UTF-8_item_6_35
  • UTF-8 is self-synchronizing: character boundaries are easily identified by scanning for well-defined bit patterns in either direction. If bytes are lost due to error or corruption, one can always locate the next valid character and resume processing. If there is a need to shorten a string to fit a specified field, the previous valid character can easily be found. Many multi-byte encodings such as Shift JIS are much harder to resynchronize. This also means that byte-oriented string-searching algorithms can be used with UTF-8 (as a character is the same as a "word" made up of that many bytes), optimized versions of byte searches can be much faster due to hardware support and lookup tables that have only 256 entries. Self-synchronization does however require that bits be reserved for these markers in every byte, increasing the size.UTF-8_item_6_36
  • Efficient to encode using simple bitwise operations. UTF-8 does not require slower mathematical operations such as multiplication or division (unlike Shift JIS, GB 2312 and other encodings).UTF-8_item_6_37
  • UTF-8 will take more space than a multi-byte encoding designed for a specific script. East Asian legacy encodings generally used two bytes per character yet take three bytes per character in UTF-8.UTF-8_item_6_38

UTF-16 UTF-8_section_14

UTF-8_unordered_list_7

  • Byte encodings and UTF-8 are represented by byte arrays in programs, and often nothing needs to be done to a function when converting source code from a byte encoding to UTF-8. UTF-16 is represented by 16-bit word arrays, and converting to UTF-16 while maintaining compatibility with existing ASCII-based programs (such as was done with Windows) requires every API and data structure that takes a string to be duplicated, one version accepting byte strings and another version accepting UTF-16. If backward compatibility is not needed, all string handling still must be modified.UTF-8_item_7_39
  • Text encoded in UTF-8 will be smaller than the same text encoded in UTF-16 if there are more code points below U+0080 than in the range U+0800..U+FFFF. This is true for all modern European languages.UTF-8_item_7_40
    • Text in (for example) Chinese, Japanese or Devanagari will take more space in UTF-8 if there are more of these characters than there are ASCII characters. This is likely when data mainly consist of pure prose, but is lessened by the degree to which the context uses ASCII whitespace, digits, and punctuation.UTF-8_item_7_41
    • Most of the rich text formats (including HTML) contain a large proportion of ASCII characters for the sake of formatting, thus the size usually will be reduced significantly compared with UTF-16, even when the language mostly uses 3-byte long characters in UTF-8.UTF-8_item_7_42
  • Most communication (e.g. HTML and IP) and storage (e.g. for Unix) was designed for a stream of bytes. A UTF-16 string must use a pair of bytes for each code unit:UTF-8_item_7_43
    • The order of those two bytes becomes an issue and must be specified in the UTF-16 protocol, such as with a byte order mark.UTF-8_item_7_44
    • If an odd number of bytes is missing from UTF-16, the whole rest of the string will be meaningless text. Any bytes missing from UTF-8 will still allow the text to be recovered accurately starting with the next character after the missing bytes.UTF-8_item_7_45

Derivatives UTF-8_section_15

The following implementations show slight differences from the UTF-8 specification. UTF-8_sentence_128

They are incompatible with the UTF-8 specification and may be rejected by conforming UTF-8 applications. UTF-8_sentence_129

CESU-8 UTF-8_section_16

Main article: CESU-8 UTF-8_sentence_130

Unicode Technical Report #26 assigns the name CESU-8 to a nonstandard variant of UTF-8, in which Unicode characters in supplementary planes are encoded using six bytes, rather than the four bytes required by UTF-8. UTF-8_sentence_131

CESU-8 encoding treats each half of a four-byte UTF-16 surrogate pair as a two-byte UCS-2 character, yielding two three-byte UTF-8 characters, which together represent the original supplementary character. UTF-8_sentence_132

Unicode characters within the Basic Multilingual Plane appear as they would normally in UTF-8. UTF-8_sentence_133

The Report was written to acknowledge and formalize the existence of data encoded as CESU-8, despite the Unicode Consortium discouraging its use, and notes that a possible intentional reason for CESU-8 encoding is preservation of UTF-16 binary collation. UTF-8_sentence_134

CESU-8 encoding can result from converting UTF-16 data with supplementary characters to UTF-8, using conversion methods that assume UCS-2 data, meaning they are unaware of four-byte UTF-16 supplementary characters. UTF-8_sentence_135

It is primarily an issue on operating systems which extensively use UTF-16 internally, such as Microsoft Windows. UTF-8_sentence_136

In Oracle Database, the UTF8 character set uses CESU-8 encoding, and is deprecated. UTF-8_sentence_137

The AL32UTF8 character set uses standards-compliant UTF-8 encoding, and is preferred. UTF-8_sentence_138

CESU-8 is prohibited for use in HTML5 documents. UTF-8_sentence_139

MySQL utf8mb3 UTF-8_section_17

In MySQL, the utf8mb3 character set is defined to be UTF-8 encoded data with a maximum of three bytes per character, meaning only Unicode characters in the Basic Multilingual Plane are supported. UTF-8_sentence_140

Unicode characters in supplementary planes are explicitly not supported. UTF-8_sentence_141

utf8mb3 is deprecated in favor of the utf8mb4 character set, which uses standards-compliant UTF-8 encoding. UTF-8_sentence_142

utf8 is an alias for utf8mb3, but is intended to become an alias to utf8mb4 in a future release of MySQL. UTF-8_sentence_143

It is possible, though unsupported, to store CESU-8 encoded data in utf8mb3, by handling UTF-16 data with supplementary characters as though it is UCS-2. UTF-8_sentence_144

Modified UTF-8 UTF-8_section_18

Modified UTF-8 (MUTF-8) originated in the Java programming language. UTF-8_sentence_145

In Modified UTF-8, the null character (U+0000) uses the two-byte overlong encoding 11000000 10000000 (hexadecimal C0 80), instead of 00000000 (hexadecimal 00). UTF-8_sentence_146

Modified UTF-8 strings never contain any actual null bytes but can contain all Unicode code points including U+0000, which allows such strings (with a null byte appended) to be processed by traditional null-terminated string functions. UTF-8_sentence_147

All known Modified UTF-8 implementations also treat the surrogate pairs as in CESU-8. UTF-8_sentence_148

In normal usage, the language supports standard UTF-8 when reading and writing strings through and (if it is the platform's default character set or as requested by the program). UTF-8_sentence_149

However it uses Modified UTF-8 for object serialization among other applications of and , for the Java Native Interface, and for embedding constant strings in . UTF-8_sentence_150

The dex format defined by Dalvik also uses the same modified UTF-8 to represent string values. UTF-8_sentence_151

Tcl also uses the same modified UTF-8 as Java for internal representation of Unicode data, but uses strict CESU-8 for external data. UTF-8_sentence_152

WTF-8 UTF-8_section_19

In WTF-8 (Wobbly Transformation Format, 8-bit) unpaired surrogate halves (U+D800 through U+DFFF) are allowed. UTF-8_sentence_153

This is necessary to store possibly-invalid UTF-16, such as Windows filenames. UTF-8_sentence_154

Many systems that deal with UTF-8 work this way without considering it a different encoding, as it is simpler. UTF-8_sentence_155

(The term "WTF-8" has also been used humorously to refer to erroneously doubly-encoded UTF-8 sometimes with the implication that CP1252 bytes are the only ones encoded) UTF-8_sentence_156

PEP 383 UTF-8_section_20

Version 3 of the Python programming language treats each byte of an invalid UTF-8 bytestream as an error; this gives 128 different possible errors. UTF-8_sentence_157

Extensions have been created to allow any byte sequence that is assumed to be UTF-8 to be lossless transformed to UTF-16 or UTF-32, by translating the 128 possible error bytes to reserved code points, and transforming those code points back to error bytes to output UTF-8. UTF-8_sentence_158

The most common approach is to translate the codes to U+DC80...U+DCFF which are low (trailing) surrogate values and thus "invalid" UTF-16, as used by Python's PEP 383 (or "surrogateescape") approach. UTF-8_sentence_159

Another encoding called MirBSD OPTU-8/16 converts them to U+EF80...U+EFFF in a Private Use Area. UTF-8_sentence_160

In either approach, the byte value is encoded in the low eight bits of the output code point. UTF-8_sentence_161

These encodings are very useful because they avoid the need to deal with "invalid" byte strings until much later, if at all, and allow "text" and "data" byte arrays to be the same object. UTF-8_sentence_162

If a program wants to use UTF-16 internally these are required to preserve and use filenames that can use invalid UTF-8; as the Windows filesystem API uses UTF-16, the need to support invalid UTF-8 is less there. UTF-8_sentence_163

For the encoding to be reversible, the standard UTF-8 encodings of the code points used for erroneous bytes must be considered invalid. UTF-8_sentence_164

This makes the encoding incompatible with WTF-8 or CESU-8 (though only for 128 code points). UTF-8_sentence_165

When re-encoding it is necessary to be careful of sequences of error code points which convert back to valid UTF-8, which may be used by malicious software to get unexpected characters in the output, though this cannot produce ASCII characters so it is considered comparatively safe, since malicious sequences (such as cross-site scripting) usually rely on ASCII characters. UTF-8_sentence_166

See also UTF-8_section_21

UTF-8_unordered_list_8


Credits to the contents of this page go to the authors of the corresponding Wikipedia page: en.wikipedia.org/wiki/UTF-8.