byte[] to string

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.
 
N

NuTcAsE

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
 
P

Pete Davis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top