Detect encoding of byte array C# Detect encoding of byte array C# arrays arrays

Detect encoding of byte array C#


In short, no. Please see How to detect the character encoding of a text file? for a detailed answer on various encodings and why they can't be automatically determined.

Your best solution is to convert the string from it's original encoding to UTF8 and convert that to a byte array. Then you'll know your byte array's encoding...


I realize I'm late to the party here, but I just had a need to do this very thing and found a good way to do it:

byte[] data; // Populate this however you see fit with your datastring text;Encoding enc;using (StreamReader reader = new StreamReader(new MemoryStream(data),                                               detectEncodingFromByteOrderMarks: true)){    text = reader.ReadToEnd();    enc = reader.CurrentEncoding; // the reader detects the encoding for you!}


Complementing other response, you could try do:

string str = BitConverter.ToString(byte_array);byte[] byte_array = Encoding.UTF8.GetBytes(str);