admin管理员组

文章数量:1426491

I have been working with node's fs.readFileSync(), passing "utf8" as the encoding to read input. When the file contains a BOM character in UTF8 (0xEF 0xBF 0xBB) it converts it to the byte sequence 0xFE 0xFF instead, which is the Unicode encoding.

Why does it do this? Why not keep the origin sequence for BOMs in UTF8?

I have been working with node's fs.readFileSync(), passing "utf8" as the encoding to read input. When the file contains a BOM character in UTF8 (0xEF 0xBF 0xBB) it converts it to the byte sequence 0xFE 0xFF instead, which is the Unicode encoding.

Why does it do this? Why not keep the origin sequence for BOMs in UTF8?

Share Improve this question edited Jul 20, 2013 at 1:05 mor 2,31318 silver badges28 bronze badges asked Jul 20, 2013 at 0:52 Rick EyreRick Eyre 2,5054 gold badges21 silver badges27 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

The BOM is character U+FEFF. 0xEF 0xBB 0xBF is its UTF-8 representation. But by reading with an encoding of utf8, you're decoding UTF-8. At this point it bees meaningless to talk about a "byte sequence"; you have a string of characters, the first of which is U+FEFF.

本文标签: javascriptWhy does Nodejs convert BOM character to 0xFE 0xFFStack Overflow