venerdì 14 novembre 2008

How to convert string to byte array and viceversa

The way to convert a string to byte array are a lot. Here I show you one I use over all, UnicodeEncoding.
Sample:

char[] arrUnicodeSample = new char[] { '\uAAAA', '\uBBBB', '\uCCCC', '\uDFFF' };
string strUnicode = new string(arrUnicodeSample);

{
UnicodeEncoding encUnicode = new UnicodeEncoding ();
byte[] encodedBytes = encUnicode.GetBytes(strUnicode);
String decodedString = encUnicode.GetString(encodedBytes, 0, encodedBytes.Length);
}

You can also use:
  • Encoding
  • ASCIIEncoding
  • UnicodeEncoding
and Decoder to come back.

Nessun commento: