{
    "mode": "perldoc",
    "parameter": "Encode::Unicode",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Encode%3A%3AUnicode/json",
    "generated": "2026-06-16T10:28:58Z",
    "synopsis": "use Encode qw/encode decode/;\n$ucs2 = encode(\"UCS-2BE\", $utf8);\n$utf8 = decode(\"UCS-2BE\", $ucs2);",
    "sections": {
        "NAME": {
            "content": "Encode::Unicode -- Various Unicode Transformation Formats\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Encode qw/encode decode/;\n$ucs2 = encode(\"UCS-2BE\", $utf8);\n$utf8 = decode(\"UCS-2BE\", $ucs2);\n",
            "subsections": []
        },
        "ABSTRACT": {
            "content": "This module implements all Character Encoding Schemes of Unicode that are officially documented\nby Unicode Consortium (except, of course, for UTF-8, which is a native format in perl).\n\n<http://www.unicode.org/glossary/> says:\n*Character Encoding Scheme* A character encoding form plus byte serialization. There are\nSeven character encoding schemes in Unicode: UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32\n(UCS-4), UTF-32BE (UCS-4BE) and UTF-32LE (UCS-4LE), and UTF-7.\n\nSince UTF-7 is a 7-bit (re)encoded version of UTF-16BE, It is not part of Unicode's\nCharacter Encoding Scheme. It is separately implemented in Encode::Unicode::UTF7. For\ndetails see Encode::Unicode::UTF7.\n\nQuick Reference\nDecodes from ord(N)           Encodes chr(N) to...\noctet/char BOM S.P d800-dfff  ord > 0xffff     \\x{1abcd} ==\n---------------+-----------------+------------------------------\nUCS-2BE       2   N   N  is bogus                  Not Available\nUCS-2LE       2   N   N     bogus                  Not Available\nUTF-16      2/4   Y   Y  is   S.P           S.P            BE/LE\nUTF-16BE    2/4   N   Y       S.P           S.P    0xd82a,0xdfcd\nUTF-16LE    2/4   N   Y       S.P           S.P    0x2ad8,0xcddf\nUTF-32        4   Y   -  is bogus         As is            BE/LE\nUTF-32BE      4   N   -     bogus         As is       0x0001abcd\nUTF-32LE      4   N   -     bogus         As is       0xcdab0100\nUTF-8       1-4   -   -     bogus   >= 4 octets   \\xf0\\x9a\\af\\8d\n---------------+-----------------+------------------------------\n",
            "subsections": []
        },
        "Size, Endianness, and BOM": {
            "content": "You can categorize these CES by 3 criteria: size of each character, endianness, and Byte Order\nMark.\n\nby size\nUCS-2 is a fixed-length encoding with each character taking 16 bits. It does not support\n*surrogate pairs*. When a surrogate pair is encountered during decode(), its place is filled\nwith \\x{FFFD} if *CHECK* is 0, or the routine croaks if *CHECK* is 1. When a character whose ord\nvalue is larger than 0xFFFF is encountered, its place is filled with \\x{FFFD} if *CHECK* is 0,\nor the routine croaks if *CHECK* is 1.\n\nUTF-16 is almost the same as UCS-2 but it supports *surrogate pairs*. When it encounters a high\nsurrogate (0xD800-0xDBFF), it fetches the following low surrogate (0xDC00-0xDFFF) and\n\"desurrogate\"s them to form a character. Bogus surrogates result in death. When \\x{10000} or\nabove is encountered during encode(), it \"ensurrogate\"s them and pushes the surrogate pair to\nthe output stream.\n\nUTF-32 (UCS-4) is a fixed-length encoding with each character taking 32 bits. Since it is\n32-bit, there is no need for *surrogate pairs*.\n\nby endianness\nThe first (and now failed) goal of Unicode was to map all character repertoires into a\nfixed-length integer so that programmers are happy. Since each character is either a *short* or\n*long* in C, you have to pay attention to the endianness of each platform when you pass data to\none another.\n\nAnything marked as BE is Big Endian (or network byte order) and LE is Little Endian (aka VAX\nbyte order). For anything not marked either BE or LE, a character called Byte Order Mark (BOM)\nindicating the endianness is prepended to the string.\n\nCAVEAT: Though BOM in utf8 (\\xEF\\xBB\\xBF) is valid, it is meaningless and as of this writing\nEncode suite just leave it as is (\\x{FeFF}).\n\nBOM as integer when fetched in network byte order\n16         32 bits/char\n-------------------------\nBE      0xFeFF 0x0000FeFF\nLE      0xFFFe 0xFFFe0000\n-------------------------\n\nThis modules handles the BOM as follows.\n\n*   When BE or LE is explicitly stated as the name of encoding, BOM is simply treated as a\nnormal character (ZERO WIDTH NO-BREAK SPACE).\n\n*   When BE or LE is omitted during decode(), it checks if BOM is at the beginning of the\nstring; if one is found, the endianness is set to what the BOM says.\n\n*   Default Byte Order\n\nWhen no BOM is found, Encode 2.76 and blow croaked. Since Encode 2.77, it falls back to BE\naccordingly to RFC2781 and the Unicode Standard version 8.0\n\n*   When BE or LE is omitted during encode(), it returns a BE-encoded string with BOM prepended.\nSo when you want to encode a whole text file, make sure you encode() the whole text at once,\nnot line by line or each line, not file, will have a BOM prepended.\n\n*   \"UCS-2\" is an exception. Unlike others, this is an alias of UCS-2BE. UCS-2 is already\nregistered by IANA and others that way.\n",
            "subsections": []
        },
        "Surrogate Pairs": {
            "content": "To say the least, surrogate pairs were the biggest mistake of the Unicode Consortium. But\naccording to the late Douglas Adams in *The Hitchhiker's Guide to the Galaxy* Trilogy, \"In the\nbeginning the Universe was created. This has made a lot of people very angry and been widely\nregarded as a bad move\". Their mistake was not of this magnitude so let's forgive them.\n\n(I don't dare make any comparison with Unicode Consortium and the Vogons here ;) Or, comparing\nEncode to Babel Fish is completely appropriate -- if you can only stick this into your ear :)\n\nSurrogate pairs were born when the Unicode Consortium finally admitted that 16 bits were not big\nenough to hold all the world's character repertoires. But they already made UCS-2 16-bit. What\ndo we do?\n\nBack then, the range 0xD800-0xDFFF was not allocated. Let's split that range in half and use the\nfirst half to represent the \"upper half of a character\" and the second half to represent the\n\"lower half of a character\". That way, you can represent 1024 * 1024 = 1048576 more characters.\nNow we can store character ranges up to \\x{10ffff} even with 16-bit encodings. This pair of\nhalf-character is now called a *surrogate pair* and UTF-16 is the name of the encoding that\nembraces them.\n\nHere is a formula to ensurrogate a Unicode character \\x{10000} and above;\n\n$hi = ($uni - 0x10000) / 0x400 + 0xD800;\n$lo = ($uni - 0x10000) % 0x400 + 0xDC00;\n\nAnd to desurrogate;\n\n$uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);\n\nNote this move has made \\x{D800}-\\x{DFFF} into a forbidden zone but perl does not prohibit the\nuse of characters within this range. To perl, every one of \\x{00000000} up to \\x{ffffffff} (*)\nis *a character*.\n\n(*) or \\x{ffffffffffffffff} if your perl is compiled with 64-bit\ninteger support!\n",
            "subsections": []
        },
        "Error Checking": {
            "content": "Unlike most encodings which accept various ways to handle errors, Unicode encodings simply\ncroaks.\n\n% perl -MEncode -e'$ = \"\\xfe\\xff\\xd8\\xd9\\xda\\xdb\\0\\n\"' \\\n-e'Encode::fromto($, \"utf16\",\"shiftjis\", 0); print'\nUTF-16:Malformed LO surrogate d8d9 at /path/to/Encode.pm line 184.\n% perl -MEncode -e'$a = \"BOM missing\"' \\\n-e' Encode::fromto($a, \"utf16\", \"shiftjis\", 0); print'\nUTF-16:Unrecognised BOM 424f at /path/to/Encode.pm line 184.\n\nUnlike other encodings where mappings are not one-to-one against Unicode, UTFs are supposed to\nmap 100% against one another. So Encode is more strict on UTFs.\n\nConsider that \"division by zero\" of Encode :)\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Encode, Encode::Unicode::UTF7, <http://www.unicode.org/glossary/>,\n<http://www.unicode.org/faq/utfbom.html>,\n\nRFC 2781 <http://www.ietf.org/rfc/rfc2781.txt>,\n\nThe whole Unicode standard <http://www.unicode.org/unicode/uni2book/u2.html>\n\nCh. 15, pp. 403 of \"Programming Perl (3rd Edition)\" by Larry Wall, Tom Christiansen, Jon Orwant;\nO'Reilly & Associates; ISBN 0-596-00027-8\n",
            "subsections": []
        }
    },
    "summary": "Encode::Unicode -- Various Unicode Transformation Formats",
    "flags": [],
    "examples": [],
    "see_also": []
}