Encode::Unicode — Various Unicode Transformation Formats
| Encoding | Size (bytes) | BOM | Surrogate Pairs | Decodes surrogate pair | Encodes >0xFFFF | Example (0x1ABCD) |
|---|---|---|---|---|---|---|
| UCS-2BE | 2 | N | N | is bogus | Not Available | — |
| UCS-2LE | 2 | N | N | bogus | Not Available | — |
| UTF-16 | 2/4 | Y | Y | is S.P | S.P | BE/LE |
| UTF-16BE | 2/4 | N | Y | S.P | S.P | 0xd82a,0xdfcd |
| UTF-16LE | 2/4 | N | Y | S.P | S.P | 0x2ad8,0xcddf |
| UTF-32 | 4 | Y | — | is bogus | As is | BE/LE |
| UTF-32BE | 4 | N | — | bogus | As is | 0x0001abcd |
| UTF-32LE | 4 | N | — | bogus | As is | 0xcdab0100 |
| UTF-8 | 1-4 | — | — | bogus | >= 4 octets | \xf0\x9a\xaf\x8d |
use Encode qw/encode decode/;
$ucs2 = encode("UCS-2BE", $utf8);
$utf8 = decode("UCS-2BE", $ucs2);
This module implements all Character Encoding Schemes of Unicode that are officially documented by Unicode Consortium (except, of course, for UTF-8, which is a native format in perl).
http://www.unicode.org/glossary/ says:
Character Encoding Scheme A character encoding form plus byte serialization. There are seven character encoding schemes in Unicode: UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32 (UCS-4), UTF-32BE (UCS-4BE) and UTF-32LE (UCS-4LE), and UTF-7.
Since UTF-7 is a 7-bit (re)encoded version of UTF-16BE, It is not part of Unicode's Character Encoding Scheme. It is separately implemented in Encode::Unicode::UTF7. For details see Encode::Unicode::UTF7.
You can categorize these CES by 3 criteria: size of each character, endianness, and Byte Order Mark.
\x{FFFD} if CHECK is 0, or the routine croaks if CHECK is 1. When a character whose ord value is larger than 0xFFFF is encountered, its place is filled with \x{FFFD} if CHECK is 0, or the routine croaks if CHECK is 1.\x{10000} or above is encountered during encode(), it “ensurrogate”s them and pushes the surrogate pair to the output stream.The first (and now failed) goal of Unicode was to map all character repertoires into a fixed-length integer so that programmers are happy. Since each character is either a short or long in C, you have to pay attention to the endianness of each platform when you pass data to one another.
CAVEAT: Though BOM in utf8 (\xEF\xBB\xBF) is valid, it is meaningless and as of this writing Encode suite just leave it as is (\x{FeFF}).
| Endianness | 16 bits/char | 32 bits/char |
|---|---|---|
| BE | 0xFeFF | 0x0000FeFF |
| LE | 0xFFFe | 0xFFFe0000 |
To say the least, surrogate pairs were the biggest mistake of the Unicode Consortium. But according to the late Douglas Adams in The Hitchhiker's Guide to the Galaxy Trilogy, “In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move”. Their mistake was not of this magnitude so let's forgive them.
(I don't dare make any comparison with Unicode Consortium and the Vogons here ;) Or, comparing Encode to Babel Fish is completely appropriate — if you can only stick this into your ear :)
Surrogate pairs were born when the Unicode Consortium finally admitted that 16 bits were not big enough to hold all the world's character repertoires. But they already made UCS-2 16-bit. What do we do?
Back then, the range 0xD800–0xDFFF was not allocated. Let's split that range in half and use the first half to represent the “upper half of a character” and the second half to represent the “lower half of a character”. That way, you can represent 1024 × 1024 = 1,048,576 more characters. Now we can store character ranges up to \x{10ffff} even with 16-bit encodings. This pair of half-character is now called a surrogate pair and UTF-16 is the name of the encoding that embraces them.
Here is a formula to ensurrogate a Unicode character \x{10000} and above:
$hi = ($uni - 0x10000) / 0x400 + 0xD800;
$lo = ($uni - 0x10000) % 0x400 + 0xDC00;
And to desurrogate:
$uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00);
Note this move has made \x{D800}-\x{DFFF} into a forbidden zone but perl does not prohibit the use of characters within this range. To perl, every one of \x{0000_0000} up to \x{ffff_ffff} (*) is a character.
(*) or \x{ffff_ffff_ffff_ffff} if your perl is compiled with 64-bit integer support!
Unlike most encodings which accept various ways to handle errors, Unicode encodings simply croak.
% perl -MEncode -e'$_ = "\xfe\xff\xd8\xd9\xda\xdb\0\n"' \
-e'Encode::from_to($_, "utf16","shift_jis", 0); print'
UTF-16:Malformed LO surrogate d8d9 at /path/to/Encode.pm line 184.
% perl -MEncode -e'$a = "BOM missing"' \
-e'Encode::from_to($a, "utf16", "shift_jis", 0); print'
UTF-16:Unrecognised BOM 424f at /path/to/Encode.pm line 184.
Unlike other encodings where mappings are not one-to-one against Unicode, UTFs are supposed to map 100% against one another. So Encode is more strict on UTFs.
Consider that “division by zero” of Encode :)
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-12 05:02 @216.73.217.145
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)