Add Unicode Encoding Preamble Byte Order Mark (BOM) in C#

The Byte Order Mark (BOM) is a unicode character situated at the start of a text stream which indicates the byte order and unicode encoding the stream is encoded as.

The following code shows one way to add this in C#:

Encoding unicode = new UnicodeEncoding();
byte[] bom = unicode.GetPreamble();
byte[] encodedBytes = unicode.GetBytes("Text I want to encode");
byte[] combinedBytes = new byte[bom.Length + encodedBytes.Length];
System.Buffer.BlockCopy(bom, 0, combinedBytes, 0, bom.Length);
System.Buffer.BlockCopy(encodedBytes, 0, combinedBytes, bom.Length, encodedBytes.Length);

The preamble is obtained from the UnicodeEncoding object using the GetPreamble method.  The encoded byte array is then concatenated with the preamble using System.Buffer.BlockCopy.

You can then write the byte array out to a file and the byte order mark will be included at the start.

Comments

Popular posts from this blog

LG TV This app will now restart to free up more memory

LG TV Clear All Browsing History Data

Excel Import CSV not using "Use First Row as Headers"