Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read binary data in C# .NET and then convert it to a string?

As opposed to using StreamReader/Filestream I want to read binary data from files and show that data (formatted) in a textbox.

like image 745
liamzebedee Avatar asked Dec 07 '22 22:12

liamzebedee


1 Answers

So binary data as in potentially non-printable data? Well if you want to print the data out as a hex string, take the data as an array of bytes then convert to a hex representation.

string path = @"path\to\my\file";
byte[] data = File.ReadAllBytes(path);
string dataString = String.Concat(data.Select(b => b.ToString("x2")));
textBox.Text = dataString;
like image 131
Jeff Mercado Avatar answered Dec 21 '22 23:12

Jeff Mercado