Byte order mark

## Byte order mark

The byte order mark (BOM) is a ​​Unicode​​​ character used to signal the ​​endianness​​​ (byte order) of a text file or stream. Its code point is ​​U+FEFF​​​. BOM use is optional, and, if used, should appear at the start of the text stream. Beyond its specific use as a byte-order indicator, the BOM character may also indicate which of the several Unicode representations the text is encoded in.​[1]

Because Unicode can be encoded as 16-bit or 32-bit integers, a computer receiving these encodings from arbitrary sources needs to know which byte order the integers are encoded in. The BOM gives the producer of the text a way to describe the text stream's endianness to the consumer of the text without requiring some contract or metadata outside of the text stream itself. Once the receiving computer has consumed the text stream, it presumably processes the characters in its own native byte order and no longer needs the BOM. Hence the need for a BOM arises in the context of text interchange, rather than in normal text processing within a closed environment.

[​​hide​​]

## [​​edit​​]Usage

If the BOM character appears in the middle of a data stream, Unicode says it should be interpreted as a "​​zero-width non-breaking space​​​" (essentially a null character). In Unicode 3.2, this usage is deprecated in favour of the "Word Joiner" character, U+2060.​[1]​ This allows U+FEFF to be only used as a BOM.

### [​​edit​​]UTF-8

The UTF-8 representation of the BOM is the byte sequence ​​0xEF,0xBB,0xBF​​​. A text editor or web browser interpreting the text as ​​ISO-8859-1​​​ or ​​CP1252​​​ will display the characters ​​ï»¿​​ for this.

The Unicode Standard does permit the BOM in ​​UTF-8​​​,​[2]​​ but does not require or recommend its use.​[3]​​ Byte order has no meaning in UTF-8​[4]​ so in UTF-8 the BOM serves only to identify a text stream or file as UTF-8.

A reason the UTF-8 BOM is recommended against is that many pieces of software without Unicode support nevertheless are able to handle UTF-8 inside a text but not at the start of a text. For instance, the bytes of UTF-8 can be placed between the quotes of string constants in many programming languages, and that language will write the correct UTF-8 to a file or to a display, despite the language not knowing anything about UTF-8. This provides an easy migration path to convert systems to Unicode and to remove all legacy encodings, without simultaneously upgrading the programming language. The unexpected three bytes of the BOM break this however, as they are located where they are certain to be a syntax error.

A leading BOM can also defeat software that uses pattern matching on the start of a text file, since it inserts 3 bytes before the pattern. Though commonly associated with the Unix ​​shebang​​​ at the start of an interpreted script,​[5]​​ the problem is more widespread. For instance in ​​PHP​​, the existence of a BOM will cause the page to begin output before the initial code is interpreted, causing problems if the page is trying to send custom HTTP headers (which must be set before output begins).

Many ​​Windows​​​ programs (including Windows ​​Notepad​​) add BOMs to UTF-8 files by default.

### [​​edit​​]UTF-16

In ​​UTF-16​​​, a BOM (​​U+FEFF​​) may be placed as the first character of a file or character stream to indicate the endianness (byte order) of all the 16-bit code units of the file or stream.

• If the 16-bit units are represented in ​​big-endian​​​ byte order, this BOM character will appear in the sequence of bytes as ​​0xFE​​​ followed by ​​0xFF​​​. This sequence appears as the ​​ISO-8859-1​​​ characters ​​þÿ​​ in a text display that expects the text to be ISO-8859-1, although UTF-16 text will be more or less unreadable if the text display doesn't support UTF-16.
• if the 16-bit units use ​​little-endian​​​ order, the sequence of bytes will have ​​0xFF​​​ followed by ​​0xFE​​​. This sequence appears as the ​​ISO-8859-1​​​ characters ​​ÿþ​​ in a text display that expects the text to be ISO-8859-1.

Programs expecting UTF-8 may show these or error indicators, depending on how they handle UTF-8 encoding errors. In all cases they will probably display the rest of the file as garbage (a UTF-16 text containing ASCII only will be fairly readable).

For the ​​IANA​​ registered charsets UTF-16BE and UTF-16LE, a byte order mark should not be used because the names of these character sets already determine the byte order. If encountered anywhere in such a text stream, U+FEFF is to be interpreted as a "zero width no-break space".

Clause D98 of conformance (section 3.10) of the Unicode standard states, "The UTF-16 encoding scheme may or may not begin with a BOM. However, when there is no BOM, and in the absence of a higher-level protocol, the byte order of the UTF-16 encoding scheme is big-endian." Whether or not a higher-level protocol is in force is open to interpretation. Files local to a computer for which the native byte ordering is little-endian, for example, might be argued to be encoded as UTF-16LE implicitly. Therefore the presumption of big-endian is widely ignored. When those same files are accessible on the Internet, on the other hand, no such presumption can be made. Searching for ASCII characters or just the space character (U+0020) is a method of determining the UTF-16 byte order.

### [​​edit​​]UTF-32

Although a BOM could be used with ​​UTF-32​​​, this encoding is rarely used for transmission. Otherwise the same rules as for ​​UTF-16​​ are applicable.

## [​​edit​​]Representations of byte order marks by encoding

 Encoding Representation (​​hexadecimal​​) Representation (​​decimal​​) Representation (​​ISO-8859-1​​) ​​UTF-8​​​​[t 1]​​ ​​EF BB BF​​ ​​239 187 191​​ ​​ï»¿​​ ​​UTF-16​​ (​​BE​​) ​​FE FF​​ ​​254 255​​ ​​þÿ​​ ​​UTF-16​​ (​​LE​​) ​​FF FE​​ ​​255 254​​ ​​ÿþ​​ ​​UTF-32​​ (BE) ​​00 00 FE FF​​ ​​0 0 254 255​​ ​​□□þÿ​​ (□ is the ascii null character) ​​UTF-32​​ (LE) ​​FF FE 00 00​​ ​​255 254 0 0​​ ​​ÿþ□□​​ (□ is the ascii null character) ​​UTF-7​​​​[t 1]​​ ​​2B 2F 76 382B 2F 76 392B 2F 76 2B2B 2F 76 2F​​​​[t 2]​​ ​​43 47 118 5643 47 118 5743 47 118 4343 47 118 47​​ ​​+/v8+/v9+/v++/v/​​ ​​UTF-1​​​​[t 1]​​ ​​F7 64 4C​​ ​​247 100 76​​ ​​÷dL​​ ​​DD 73 66 73​​ ​​221 115 102 115​​ ​​Ýsfs​​ ​​SCSU​​​​[t 1]​​ ​​0E FE FF​​​​[t 3]​​ ​​14 254 255​​ ​​□þÿ​​ (□ is the ascii "shift out" character) ​​BOCU-1​​​​[t 1]​​ ​​FB EE 28​​ ​​251 238 40​​ ​​ûî​​ ​​84 31 95 33​​ ​​132 49 149 51​​ ​​□1■3​​ (□ and ■ are unmapped ISO-8859-1 characters)

1. ^ ​a​​ ​b​​ ​c​​ ​d​​ ​e​​ ​f​​ ​g​​ This is not really a "byte order" mark, since the byte is also the code unit in these encodings and there is no byte order to resolve. The sequence can be used to identify the encoding, however.​[4]​​[6]
2. ^​​ In UTF-7, the fourth byte of the BOM, before encoding as ​​base64​​​, is ​​001111xx​​​ in binary, and ​​xx​​​ depends on the next character (the first character after the BOM). Hence, technically, the fourth byte is not purely a part of the BOM, but also contains information about the next (non-BOM) character. For ​​xx=00​​​, ​​01​​​, ​​10​​​, ​​11​​​, this byte is, respectively, ​​38​​​, ​​39​​​, ​​2B​​​, or ​​2F​​​ when encoded as base64. If no following character is encoded, ​​38​​​ is used for the fourth byte and the following byte is ​​2D​​.
3. ^​​ SCSU allows other encodings of U+FEFF, the shown form is the signature recommended in UTR #6.​[7]