String can be converted do byte array in few different ways, due to the following fact: .NET supports Unicode, and Unicode standardizes several difference encodings called UTFs. They have different lengths of byte representation but are equivalent in that sense that when a string is encoded, it can be coded back to the string, but if the string is encoded with one UTF and decoded in assumption of different UTF, if can be screwed up.
Also, .NET supports non-Unicode encodings, but they are not valid in general case (will be valid only if a limited sub-set of Unicode code point is used in an actual string, such as ASCII). Internally, .NET supports UTF-16, but for stream representation UTF-8 is usually used. It is also a standard-de-facto for Internet.
Not surprisingly, serialization of string into array of byte and deserialization is supported by the class
System.Text.Encoding
, which is an abstract class; its derived classes support concrete encodings:
ASCIIEncoding
and four UTFs (
System.Text.UnicodeEncoding
supports UTF-16).
See
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[
^].
For serialization to array of bytes use
System.Text.Encoding.GetBytes
. For the inverse operation use
System.Text.Encoding.GetChars
. This function returns array of characters, so to get a string, use a string constructor
System.String(char[])
.
See
http://unicode.org/[
^],
http://unicode.org/faq/utf_bom.html[
^].
[EDIT]
Example:
string myString =
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
byte[] bytes = encoding.GetBytes(myString);
myString = new string(encoding.GetChars(bytes));
byte[] bytes = encoding.GetBytes(myString);
myString = new string(encoding.GetChars(bytes));
byte[] bytes = encoding.GetBytes(myString);
—SA