C# BinaryReader

Binary: file containing binary data

BinaryReader handles binary files. A binary file may have thousands of integers stored in it, or another simple data type. BinaryReader is mainly for convenience. It makes using binary data easier.

File

First, if you do not have a binary file you are trying to open already, you can create one using the BinaryWriter type in the C# language. Please visit the BinaryWriter tutorial for instructions about creating a binary file.

BinaryWriterNote

Visual Studio can open binary files and you can look at the data. It won't be readable normally, but it gives you an idea what is happening. The file here is 48 bytes, because 12 integers of 4 bytes each are in it.

Example

Here we open the same binary file and then read in the bytes. BinaryReader here provides some useful properties that make this easier. When you run the next program, you will see all the ints recovered.

C# program that uses BinaryReader

using System;
using System.IO;

class Program
{
    static void Main()
    {
	R();
    }

    static void R()
    {
	// 1.
	using (BinaryReader b = new BinaryReader(File.Open("file.bin", FileMode.Open)))
	{
	    // 2.
	    // Position and length variables.
	    int pos = 0;
	    // 2A.
	    // Use BaseStream.
	    int length = (int)b.BaseStream.Length;
	    while (pos < length)
	    {
		// 3.
		// Read integer.
		int v = b.ReadInt32();
		Console.WriteLine(v);

		// 4.
		// Advance our position variable.
		pos += sizeof(int);
	    }
	}
    }
}

Output

1
4
6
7
11
55
777
23
266
44
82
93
Note

In this example, we see that BinaryReader receives a FileStream. This is returned by File.Open. The name of the binary file we are reading is "file.bin." It uses position and length variables.

Info:These two variables store the position in the binary file, and also the total size of the binary file.

It calls ReadInt32—this method consumes 4 bytes in the binary file and turns it into an int. It also advances the file read. It increments our position. This line adds four bytes to our position.

Squares: abstract

ReadBoolean:Reads in a true or false value. I haven't used this one, and in many programs int would be used instead.

ReadByte:Reads in a single byte. This is useful for images and archives, but ReadBytes is faster.

ReadChar, ReadChars:These seem confusing, because they vary on the encoding used. Unicode chars are not the same as ASCII ones.

ReadDecimal:Reads in 16 bytes that are a decimal value. More information on the decimal type is available.

Decimal

ReadInt16, ReadInt32, ReadInt64:2, 4 or 8 bytes are read. I had problems with ReadInt16. I think ReadInt32 is commonly useful.

ReadString:MSDN says the string is "prefixed with the length, encoded as an integer seven bits at a time."

Sizeof

In the example, I used sizeof(int). This evaluates to the integer value 4 and always will, but it might be preferable for clarity. If you can't remember how many bytes are in an integer, you may have bigger problems.

Sizeof

Performance

Performance optimization

Most programs read in much more data than they write. I found BinaryReader substantially faster than using plain text. Looking into PeekChar indicates that it performs a separate read each time it is called.

Tip:This can easily multiply the time required for the BinaryReader. PeekChar should thus be avoided.

BinaryReader.PeekChar Method: MSDN
Fragment that uses PeekChar: C#

using (BinaryReader b = new BinaryReader(File.Open("file.bin", FileMode.Open)))
{
    // Do not do this.
    while (b.PeekChar() != -1)
    {
	int v = b.ReadInt32();
    }
}

Encoding

Sometimes you can fix an encoding problem by specifying Encoding.ASCII to File.Open. I do not understand why this works but it solved one of my problems. Please find someone else for an explanation.

Example that uses Encoding.ASCII: C#

//
// If all else fails, specify Encoding.ASCII to your File.Open call.
//
using (BinaryReader b = new BinaryReader(
    File.Open("file.bin", FileMode.Open, Encoding.ASCII)))
{
    // ...
}

Summary

The C# programming language

These two classes are great for reading binary files. I showed two examples and the code to demonstrate them. I also pointed out that PeekChar is an awful method. I have worked with XML serialization and binary.

And:XML serialization is human-readable and has extensive, standardized Framework support. Binary has less standard support.


C#: File