byte[] to string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the best performing way to convert a byte array (not a signed byte
array) to a string? Currently, I convert it to char[] and then to string.
 
If the byte[] are byte representation of a string, and you want to
retrieve the string,

System.Encoding.Default.GetString (byte[])... (NOTE: you can also use
any other encoding such as UTF8)

If you just want to convert an array of bytes to string for storage or
passing it around, you can do this
string byteString = Convert.ToBase64String (byte[]);

To convert the string back to bytes
byte[] byteArr = Convert.FromBase64String (string);

Hope this helps,
NuTcAsE
 
The best way is probably using one of the Encoding derived classes,
depending on the character set you want.

For example:

ASCIIEncoding encoding = new ASCIIEncoding();

string myString = encoding.GetString(myByteArray);

Alternatively, replace ASCIIEncoding with UnicodeEncoding, UTF7Encoding or
UTF8Encoding.

Pete
 
Back
Top