HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How to convert UTF-8 byte[] to string

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howbyteconvertstringutf

Problem

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

In some debugging code, I need to convert it to a string. Is there a one-liner that will do this?

Under the covers it should be just an allocation and a memcopy, so even if it is not implemented, it should be possible.

Solution

string result = System.Text.Encoding.UTF8.GetString(byteArray);


or one of the overload if you know the length:

string result = System.Text.Encoding.UTF8.GetString(byteArray, 0, 42);

Code Snippets

string result = System.Text.Encoding.UTF8.GetString(byteArray);
string result = System.Text.Encoding.UTF8.GetString(byteArray, 0, 42);

Context

Stack Overflow Q#1003275, score: 1821

Revisions (0)

No revisions yet.