Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to convert UTF-8 byte[] to string?

I have a byte[] array that is stacked from a file that I happen to known contains UTF-8.

In some investigating code, I need to change it over to a string. Is there a joke that will do this?

Under the covers, it ought to be only a designation and a memcopy, so regardless of whether it isn't carried out, it ought to be conceivable.
by

2 Answers

akshay1995

string result = System.Text.Encoding.UTF8.GetString(byteArray);
kshitijrana14
A general solution to convert from byte array to string when you don't know the encoding:
static string BytesToStringConverted(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
}

Login / Signup to Answer the Question.