# How to read bin data

If you do not, you can explore the read in options in R. To get started, we establish a connection to a file and indicate that we will be using the connection to read in binary data.

For more details, see help file in R. Next, we use the readBin command to begin. If we think the file contains integers, we can start by reading in the first integer and hoping that the size of the integer does not require further specifications. Different platforms store binary data in different ways, and which end of a string of binary values represents the greatest values or smallest values is a difference that can yield very different results from the same set of binary values.

The binary files in the examples on this page were written using a PC, which suggests they are little-endian. When reading in binary data that may or may not have been written on a different platform, indicating an endian can be crucial.

Thus, it looks like the first integer in the file is 1. As we repeatedly use readBin commands, we will work our way through the binary file until we hit the end. If the n you specify is greater than the number of integers you specified, readBin will read and display as much as is available, so there is no danger of guessing too large an n.

Since we have already read in the first integer, this command will begin at the second. If you know have additional information about what is in your file, you should incorporate that into the readBin command.

For example, if you know that you wish to read in integers stored on 4 bytes each, you can indicate this with the size option:. Similarly, if you know that your file contains characters, complex numbers, or some other type of information, you would adjust the readBin command accordingly, changing integer to character or complex. See help readBin in R for more details. The code needed to read binary data into R is relatively easy. However, reading the data in correctly requires that you are either already familiar with your data or possess a comprehensive description of the data structure.

In the binary data file, information is stored in groups of binary digits. Each binary digit is a zero or one and eight binary digits grouped together is a byte.

In order to successfully read binary data, you must know how pieces of information have been parsed into binary. For example, if your data consists of integers, how may bytes should you interpret as representative of one integer in your data? Or if your data contains both positive and negative numbers, how can you distinguish the two? How many pieces of information do you expect to find in the binary data? Ideally, you know the answers to these questions before starting to read in the binary file.

If you do not, you can explore the read in options in R. To get started, we establish a connection to a file and indicate that we will be using the connection to read in binary data. For more details, see help file in R. Next, we use the readBin command to begin. If we think the file contains integers, we can start by reading in the first integer and hoping that the size of the integer does not require further specifications.

Different platforms store binary data in different ways, and which end of a string of binary values represents the greatest values or smallest values is a difference that can yield very different results from the same set of binary values. The binary files in the examples on this page were written using a PC, which suggests they are little-endian.