Avatar ·

How to convert a byte array (byte[]) to a string in C#?

Hello everyone! I'm a beginner in C# and working with network protocols. I have a byte array that I receive from `BinaryReader`. I need to convert it to a regular string to output to the console or save to a log. Here's an example of my code: ```csharp using (var reader = new BinaryReader(File.OpenRead("data.bin"))) { byte[] buffer = reader.ReadBytes(100); // How to turn buffer into a string now? string result = ???; Console.WriteLine(result); } ``` I tried using `Encoding.Default.GetString(buffer)`, but I end up getting gibberish instead of text. I've also heard about the `ASCIIEncoding` class, but I'm not sure which encoding to use. Please advise how to properly convert bytes to a string to preserve readable text? And what to do if I don't know the encoding in advance?