.NET: Convert a Byte Array to String


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

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.