Converting from byte to string

In all cases, the general approach is to start with a byte array, use some method to convert it to a char array, and finally convert the char[] to a string.

In the System.Text.Encoding class, there are 2 methods that can convert a byte[] to a char[], and each method has its own peculiar nuances.

1.) If you decide to use Encoding.UTF8.GetChars():

The value of each byte in the array must be 127 or less. If the byte array contains a value of 128 or greater, an exception will be thrown. The byte-to-char conversion will permanently stop whenever a byte value of 0 is encountered. For example, if your byte array consists of { 65, 66, 67, 0, 69, 70 }, your resulting character array will be { ‘A’, ‘B’, ‘C’ }. All bytes starting from the 0 value onward are discarded. If the byte array begins with a 0, then everything will be discarded.

2.) If you decide to use Encoding.UTF8.GetDecoder().Convert():

The value of each byte in the array may span the entire range of 0 thru 255, and no exceptions will be thrown. Whenever a byte value of 0 is encountered, conversion will stop and all characters from that location onward will become a null character 0 ‘\0’. However, values of 128 and above, while these will not throw exceptions, may cause non-intuitive characters to be produced because the UTF-8 standard (unlike ASCII) allows characters to be composed if more than 1 byte, as detailed here: UTF-8 - Wikipedia

Most text streams will usually contain bytes from 0 thru 127. However, I have seen cases where bytes outside this range are occasionally received, possibly due to noise, etc. and it is smart to account for them if you want to build a reliable system.