{
    "content": [
        {
            "type": "text",
            "text": "# utf8(7) (man)\n\n**Summary:** UTF-8 - an ASCII compatible multibyte Unicode encoding\n\n## See Also\n\n- locale(1)\n- nllanginfo(3)\n- setlocale(3)\n- charsets(7)\n- unicode(7)\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **DESCRIPTION** (13 lines) — 6 subsections\n  - Properties (25 lines)\n  - Encoding (29 lines)\n  - Example (8 lines)\n  - Application notes (32 lines)\n  - Security (8 lines)\n  - Standards (2 lines)\n- **SEE ALSO** (2 lines)\n- **COLOPHON** (7 lines)\n\n## Full Content\n\n### NAME\n\nUTF-8 - an ASCII compatible multibyte Unicode encoding\n\n### DESCRIPTION\n\nThe  Unicode 3.0 character set occupies a 16-bit code space.  The most obvious Unicode encod‐\ning (known as UCS-2) consists of a sequence of 16-bit words.   Such  strings  can  contain—as\npart  of  many  16-bit  characters—bytes such as '\\0' or '/', which have a special meaning in\nfilenames and other C library function arguments.  In addition, the majority  of  UNIX  tools\nexpect  ASCII  files  and  can't read 16-bit words as characters without major modifications.\nFor these reasons, UCS-2 is not a suitable external encoding of Unicode  in  filenames,  text\nfiles,  environment variables, and so on.  The ISO 10646 Universal Character Set (UCS), a su‐\nperset of Unicode, occupies an even larger code space—31 bits—and the obvious UCS-4  encoding\nfor it (a sequence of 32-bit words) has the same problems.\n\nThe  UTF-8  encoding of Unicode and UCS does not have these problems and is the common way in\nwhich Unicode is used on UNIX-style operating systems.\n\n#### Properties\n\nThe UTF-8 encoding has the following nice properties:\n\n* UCS characters 0x00000000 to 0x0000007f (the classic US-ASCII characters) are encoded  sim‐\nply  as  bytes 0x00 to 0x7f (ASCII compatibility).  This means that files and strings which\ncontain only 7-bit ASCII characters have the same encoding under both ASCII and UTF-8 .\n\n* All UCS characters greater than 0x7f are encoded as a multibyte sequence consisting only of\nbytes  in  the range 0x80 to 0xfd, so no ASCII byte can appear as part of another character\nand there are no problems with, for example,  '\\0' or '/'.\n\n* The lexicographic sorting order of UCS-4 strings is preserved.\n\n* All possible 2^31 UCS codes can be encoded using UTF-8.\n\n* The bytes 0xc0, 0xc1, 0xfe, and 0xff are never used in the UTF-8 encoding.\n\n* The first byte of a multibyte sequence which represents a single non-ASCII UCS character is\nalways  in  the  range 0xc2 to 0xfd and indicates how long this multibyte sequence is.  All\nfurther bytes in a multibyte sequence are in the range 0x80  to  0xbf.   This  allows  easy\nresynchronization and makes the encoding stateless and robust against missing bytes.\n\n* UTF-8  encoded  UCS  characters  may  be up to six bytes long, however the Unicode standard\nspecifies no characters above 0x10ffff, so Unicode characters can be only up to four  bytes\nlong in UTF-8.\n\n#### Encoding\n\nThe  following byte sequences are used to represent a character.  The sequence to be used de‐\npends on the UCS code number of the character:\n\n0x00000000 - 0x0000007F:\n0xxxxxxx\n\n0x00000080 - 0x000007FF:\n110xxxxx 10xxxxxx\n\n0x00000800 - 0x0000FFFF:\n1110xxxx 10xxxxxx 10xxxxxx\n\n0x00010000 - 0x001FFFFF:\n11110xxx 10xxxxxx 10xxxxxx 10xxxxxx\n\n0x00200000 - 0x03FFFFFF:\n111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx\n\n0x04000000 - 0x7FFFFFFF:\n1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx\n\nThe xxx bit positions are filled with the bits of the character code number in binary  repre‐\nsentation, most significant bit first (big-endian).  Only the shortest possible multibyte se‐\nquence which can represent the code number of the character can be used.\n\nThe UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well as 0xfffe and 0xffff (UCS  non‐\ncharacters)  should  not  appear  in conforming UTF-8 streams. According to RFC 3629 no point\nabove U+10FFFF should be used, which limits characters to four bytes.\n\n#### Example\n\nThe Unicode character 0xa9 = 1010 1001 (the copyright sign) is encoded in UTF-8 as\n\n11000010 10101001 = 0xc2 0xa9\n\nand character 0x2260 = 0010 0010 0110 0000 (the \"not equal\" symbol) is encoded as:\n\n11100010 10001001 10100000 = 0xe2 0x89 0xa0\n\n#### Application notes\n\nUsers have to select a UTF-8 locale, for example with\n\nexport LANG=enGB.UTF-8\n\nin order to activate the UTF-8 support in applications.\n\nApplication software that has to be aware of the used character encoding  should  always  set\nthe locale with for example\n\nsetlocale(LCCTYPE, \"\")\n\nand programmers can then test the expression\n\nstrcmp(nllanginfo(CODESET), \"UTF-8\") == 0\n\nto  determine  whether  a  UTF-8 locale has been selected and whether therefore all plaintext\nstandard input and output, terminal communication, plaintext file content, filenames and  en‐\nvironment variables are encoded in UTF-8.\n\nProgrammers accustomed to single-byte encodings such as US-ASCII or ISO 8859 have to be aware\nthat two assumptions made so far are no longer valid in UTF-8  locales.   Firstly,  a  single\nbyte  does not necessarily correspond any more to a single character.  Secondly, since modern\nterminal emulators in UTF-8 mode also support  Chinese,  Japanese,  and  Korean  double-width\ncharacters as well as nonspacing combining characters, outputting a single character does not\nnecessarily advance the cursor by one position as it did in ASCII.  Library functions such as\nmbsrtowcs(3) and wcswidth(3) should be used today to count characters and cursor positions.\n\nThe official ESC sequence to switch from an ISO 2022 encoding scheme (as used for instance by\nVT100 terminals) to UTF-8 is ESC % G (\"\\x1b%G\").   The  corresponding  return  sequence  from\nUTF-8 to ISO 2022 is ESC % @ (\"\\x1b%@\").  Other ISO 2022 sequences (such as for switching the\nG0 and G1 sets) are not applicable in UTF-8 mode.\n\n#### Security\n\nThe Unicode and UCS standards require that producers of UTF-8 shall  use  the  shortest  form\npossible,  for  example, producing a two-byte sequence with first byte 0xc0 is nonconforming.\nUnicode 3.1 has added the requirement that conforming programs must not  accept  non-shortest\nforms  in  their  input.  This is for security reasons: if user input is checked for possible\nsecurity violations, a program might check only for the ASCII version of \"/../\" or \";\" or NUL\nand  overlook  that there are many non-ASCII ways to represent these things in a non-shortest\nUTF-8 encoding.\n\n#### Standards\n\nISO/IEC 10646-1:2000, Unicode 3.1, RFC 3629, Plan 9.\n\n### SEE ALSO\n\nlocale(1), nllanginfo(3), setlocale(3), charsets(7), unicode(7)\n\n### COLOPHON\n\nThis page is part of release 5.10 of the Linux  man-pages  project.   A  description  of  the\nproject,  information about reporting bugs, and the latest version of this page, can be found\nat https://www.kernel.org/doc/man-pages/.\n\n\n\nGNU                                          2019-03-06                                     UTF-8(7)\n\n"
        }
    ],
    "structuredContent": {
        "command": "utf8",
        "section": "7",
        "mode": "man",
        "summary": "UTF-8 - an ASCII compatible multibyte Unicode encoding",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [
            {
                "name": "locale",
                "section": "1",
                "url": "https://www.chedong.com/phpMan.php/man/locale/1/json"
            },
            {
                "name": "nllanginfo",
                "section": "3",
                "url": "https://www.chedong.com/phpMan.php/man/nllanginfo/3/json"
            },
            {
                "name": "setlocale",
                "section": "3",
                "url": "https://www.chedong.com/phpMan.php/man/setlocale/3/json"
            },
            {
                "name": "charsets",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/charsets/7/json"
            },
            {
                "name": "unicode",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/unicode/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 13,
                "subsections": [
                    {
                        "name": "Properties",
                        "lines": 25
                    },
                    {
                        "name": "Encoding",
                        "lines": 29
                    },
                    {
                        "name": "Example",
                        "lines": 8
                    },
                    {
                        "name": "Application notes",
                        "lines": 32
                    },
                    {
                        "name": "Security",
                        "lines": 8
                    },
                    {
                        "name": "Standards",
                        "lines": 2
                    }
                ]
            },
            {
                "name": "SEE ALSO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COLOPHON",
                "lines": 7,
                "subsections": []
            }
        ]
    }
}