A short snippet which converts a byte array to a string, and which handles conversion of the byte order mark (BOM).
public string ConvertBytesToString(byte[] bytes) { int startByte = 0, countBytes = bytes.Length; Encoding encoding; if (bytes.Length >= 3 && bytes[0] == '\x00EF' && bytes[1] == '\x00BB' && bytes[2] == '\x00BF') encoding = Encoding.UTF8; else if (bytes.Length >= 2 && bytes[0] == '\x00FF' && bytes[1] == '\x00FE') encoding = Encoding.Unicode; else if (bytes.Length >= 2 && bytes[0] == '\x00FE' && bytes[1] == '\x00FF') encoding = Encoding.BigEndianUnicode; else encoding = Encoding.ASCII; // exclude the BOM preamble bytes var pal = encoding.GetPreamble().Length; startByte += pal; countBytes -= pal; return encoding.GetString(bytes, startByte, countBytes); }
As seen, the byte order mark bytes are excluded from the conversion, which if they are left in, could cause problems when attempting to use the string, particularly if the string is an Xml string and you attempt to load it into an XmlDocument or XPathDocument.
Published by