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

28,361

Solution 1

There are different cases when one need to read binary file, since it is unclear what you really trying to achieve here are some:

  • read random file and display as series of hex values (similar to binary file view in Visual Studio or any other binary file viewer). Berfectly covered by Jeff M's answer.
  • Reading and writing your own objects using binary serialization. Check serialization walkthrough on MSDN - http://msdn.microsoft.com/en-us/library/et91as27.aspx and read on BinaryFormatter objects.
  • Reading someone elses binary format (like JPEG, PNG, ZIP, PDF). In this case you need to know structure of the file (you can often find file format documentation) and use BinaryReader to read individual chunks of the file. For most of the common file formats it is easy to find existing libraries that allow reading such files in more convinient way. MSDN article on BinaryReader have basic usage sample also: http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx.

Solution 2

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;

Solution 3

Use BinaryReader to read the file. Then encode the byte array that read from file in base64 format and assign the base64 encoded string in the textbox

UPDATE:

The byte array that read from file can be encoded in various text encoding before assigning to textbox for display. Take a look at following namespaces in .net class that related to characters encoding format:

  • System.Text.ASCIIEncoding
  • System.Text.UTF8Encoding
  • System.Text.UnicodeEncoding
  • System.Text.UTF32Encoding
  • System.Text.UTF7Encoding

Please ensure that you know the exact encoding of the target file before making any conversion from byte array to encoded string. Or you can check that file BOM bytes.

UPDATE (2):

Please note that you can't convert non-text file (eg image file, music file) using any of System.Text class. Otherwise it is meaning-less for you to display in the textbox.

Share:
28,361
liamzebedee
Author by

liamzebedee

Software engineer with design-thinking mindset. Always learning, started programming when I was 11, moved to Sydney when I was 17, been working/learning since.

Updated on September 15, 2020

Comments

  • liamzebedee
    liamzebedee over 3 years

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

  • Random
    Random about 13 years
    This not covers different bases and encodings - can cause some trouble because of that.
  • Jeff Mercado
    Jeff Mercado about 13 years
    @Random: But nothing was specified in the question. All he said was "binary data," there is no mention of whether the data was encoded or in what string representation to get. So I interpreted that as just plain, unprintable data as a hex string. All we can do right now is make guesses at what he means until he clarifies.
  • Jeff Mercado
    Jeff Mercado about 13 years
    @Liam: Encoded as what? Specify what your exact requirements are. Something like: "I want to show the binary data as bytes in their hex representation." We cannot help you if we don't know exactly what you want to do.
  • Jeff Mercado
    Jeff Mercado about 13 years
    I don't deny that getting a base64 string is just as good as any, it's a valid representation. But your recent updates about various string encodings doesn't add anything here AFAIK. That would suggest that we're reading text from the files and not raw binary.
  • Predator
    Predator about 13 years
    @Jeff M, that info maybe helpful for beginners. Plus, binary file can be a complex mixed of many things. For example within a certain bytes range, it is utf8 text values and within another range it could be byte array for images, etc.