In this article, we will learn about situations where we may need to convert a file into a byte array. Additionally, we will learn two ways to perform the conversion in C#.

If you want to learn how to convert a byte array to a file, check out our convert byte array to a file article.

To download the source code for this article, you can visit our GitHub repository.

So let’s start.

What is a Byte Array?

In C#, a byte array is an array of 8-bit unsigned integers (bytes). By combining multiple bytes into a byte array, we can represent more complex data structures, such as text, images, or audio data.

There are several use cases in which we want to convert a file to a byte array, some of them are:

  • Storing in memory – by converting a file into a byte array, we can store the entire contents of the file in memory. This is useful if we need to process the file quickly or manipulate its contents.
  • Network transmission – since byte arrays are a compact representation of data, we can send them over the network more efficiently than larger file formats.
  • File format conversion – by converting a file into a byte array, we can manipulate its contents. This is useful for converting files from one format to another.
  • Encryption – we can do easier encryption of a file by converting it into a byte array.

Generally, a byte array is declared using the byte[] syntax:

byte[] byteArray = new byte[50];

This creates a byte array with 50 elements, each of which holds a value between 0 and 255. 

Let’s now see it in action.

Use ReadAllBytes to Convert a File

We will start by creating a console app in Visual Studio.

To illustrate how to create a byte array from a file, we need a file and a folder for our code to read. Using Visual Studio’s Solution Explorer, we add a folder named Files and a new file named CodeMaze.pdf.

Now we can focus on the code to convert our file into a byte array:

static void Main()
{
    string filePath = "Files/CodeMaze.pdf";

    var fileByteArray = ConvertToByteArray(filePath);
}

public static byte[] ConvertToByteArray(string filePath)
{
    byte[] fileByteArray = File.ReadAllBytes(filePath);

    return fileByteArray;
}

First, we create a method ConvertToByteArray. This method accepts the location of the file we want to convert and returns a byte array representation of it. Inside the method, we pass the provided filePath parameter to the File.ReadAllBytes method, which performs the conversion to a byte array.

Next, we invoke the ConvertToByteArray method in our main method, and provide a path to our file, in our case "Files/CodeMaze.pdf". Lastly, we save the method response into the fileByteArray variable, which now holds a byte array representation of CodeMaze.pdf.

In general, File.ReadAllBytes is a static method in C# with only one signature, that accepts one parameter named path. In the path parameter, we provide the location of the file we want to convert to a byte array. Behind the scenes, the method opens the provided file, loads it in memory, reads the content in a byte array, and then closes the file.

What is important to note, is that the method File.ReadAllBytes will load file content in memory all at once. 

Because of this, using the method in this way is suitable when working with smaller files. When working with larger files, we don’t want to load the whole file in memory all at once, since this can lead to memory consumption issues. 

So let’s see how we can avoid this issue.

Use a Chunked Conversion to Convert a File

When working with larger files, instead of reading it all at once, we can implement reading it in chunks:

public static byte[] ConvertToByteArrayChunked(string filePath)
{
    const int MaxChunkSizeInBytes = 2048;
    var totalBytes = 0;
    byte[] fileByteArray;
    var fileByteArrayChunk = new byte[MaxChunkSizeInBytes];
    

    using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        int bytesRead;

        while ((bytesRead = stream.Read(fileByteArrayChunk, 0, fileByteArrayChunk.Length)) > 0)
        {
            totalBytes += bytesRead;
        }

        fileByteArray = new byte[totalBytes];
        stream.Seek(0, SeekOrigin.Begin);
        stream.Read(fileByteArray, 0, totalBytes);
    }

    return fileByteArray;
}

We define a variable, MaxChunkSizeInBytes, which represents the maximum size of a chunk we want to read at once. In our case, we set it to 2048. Then, we define the totalBytes variable that will keep the total value of bytes in our file. Additionally, we define fileByteArray, an array that will hold all bytes of our file, and fileByteArrayChunk which will hold the byte array of one chunk.

Next, we open and read the file we want to process into a new FileStream object and use the variable bytesRead to keep track of how many bytes we have read. In the while loop, we read the file in increments of MaxChunkSizeInBytes bytes and store each chunk of bytes in the fileByteArrayChunk array.

Now, we are ready to populate our byte array!

First, we set the size of fileByteArray to totalBytes. Before we start reading into it, it’s important to know that once we read the whole stream, its position is left at the end. If we don’t reset the position of the stream to the beginning of the file, we will end up with an array that contains only the last chunk of the file. To avoid this, we use stream.Seek(0, SeekOrigin.Begin) to set the stream position to the beginning.

Now, we can useFileStream.Read method and get the byte array of our CodeMaze.pdf. To specify the location where the read bytes are stored, we pass in fileByteArray as the first parameter. To start reading from the start of the file, we set the second parameter, offset to 0. Lastly, we indicate the number of bytes to be read by setting the third parameter to totalBytes.

Finally, we have fileByteArray that contains a byte array representation of our file.

Conclusion

In this article, we learned what are the most common use cases in which we would want to convert a file to a byte array and the benefits of it. Also, we saw how to perform different types of conversion depending on the file size.

This content is available exclusively to members of Code's Patreon at $0 or more.