X

Detect UTF-8 or UTF-16 BOM in a Stream using C#

bom

Was detecting a file stream if it was Unicode encoded or not, so though I would share the code.

Let say you an xml file that you need to detect if it has the Unicode BOM in the file.
If you wish to know more about BOM look it up at wikipedia


//open the helloworld.xml file

Stream fs = new FileStream("helloworld.xml", FileMode.Open);

byte[] bits = new byte[3];
fs.Read(bits, 0, 3);

// UTF8 BOM is like 0xEF,0xBB,0xBF
if (bits[0] == 0xEF && bits[1] == 0xBB && bits[2] == 0xBF)
{
  //utf8 do something
}
//UTF16 BOM is like 0xFF, 0xFE
if (bits[0] == 0xFF && bits[1] == 0xFE)
{
  //utf16 do something
}

Hope that helps

Categories: .NET C#
Taswar Bhatti:
Related Post