Binary Files

Functions like Read and Write handle ordinary printable text. But in dealing with external data files or devices it is sometimes necessary to go to a lower level, and work directly with raw binary data. You can do this using BinaryRead and BinaryWrite.

BinaryRead[stream]read one byte
BinaryRead[stream,type]read an object of the specified type
BinaryRead[stream,{type1,type2,...}]read a list of objects
BinaryWrite[stream,b]write one byte
BinaryWrite[stream,{b1,b2,...}]write a sequence of bytes
BinaryWrite[stream,"string"]write the characters in a string
BinaryWrite[stream,x,type]write an object of the specified type
BinaryWrite[stream,{x1,x2,...},type]write a sequence of objects
BinaryWrite[stream,{x1,x2,...},{type1,type2,...}]
write objects of different types

Reading and writing binary data.

"Byte"8-bit unsigned integer
"Character8"8-bit character
"Character16"16-bit character
"Complex64"IEEE single-precision complex number
"Complex128"IEEE double-precision complex number
"Complex256"IEEE quad-precision complex number
"Integer8"8-bit signed integer
"Integer16"16-bit signed integer
"Integer32"32-bit signed integer
"Integer64"64-bit signed integer
"Integer128"128-bit signed integer
"Real32"IEEE single-precision real number
"Real64"IEEE double-precision real number
"Real128"IEEE quad-precision real number
"TerminatedString"null-terminated string of 8-bit characters
"UnsignedInteger8"8-bit unsigned integer
"UnsignedInteger16"16-bit unsigned integer
"UnsignedInteger32"32-bit unsigned integer
"UnsignedInteger64"64-bit unsigned integer
"UnsignedInteger128"128-bit unsigned integer

Types supported in BinaryRead and BinaryWrite.

This writes a sequence of bytes to a file.
In[1]:=
Click for copyable input
Out[1]=
BinaryWrite automatically opens a stream for the file. This closes it.
In[2]:=
Click for copyable input
This reads the first byte from the file, returning it as an integer.
In[3]:=
Click for copyable input
Out[3]=
This reads the second 8 bits in the file as a character.
In[4]:=
Click for copyable input
Out[4]=
This reads the next 32 bits as a 32-bit integer.
In[5]:=
Click for copyable input
Out[5]=

Like Read and Write, BinaryRead and BinaryWrite work with streams. But if you give a file name, they automatically open the specified file as a stream. To create a stream directly you can use OpenRead or OpenWrite. On some computer systems, the option setting BinaryFormat->True is required for any stream to be used with BinaryRead and BinaryWrite, in order to prevent possible corruption from such issues as newline translation.

In using Mathematica you are normally completely insulated from the raw representation of data inside your computer. But with BinaryRead and BinaryWrite this is no longer so. One of the subtleties that then arises is that different computers may take the bytes that make up numbers to be in different orders, as specified by their setting for $ByteOrdering.

This writes a 32-bit integer to a file.
In[6]:=
Click for copyable input
Out[6]=
This closes the file.
In[7]:=
Click for copyable input
This reads the integer back, but assumes an opposite byte ordering.
In[8]:=
Click for copyable input
Out[8]=
BinaryReadList["file"]read all the bytes in a file
BinaryReadList["file",type]read all the data, treating it as objects of a certain type
BinaryReadList["file",{type1,type2,...}]treat the data as objects of a sequence of types
BinaryReadList["file",types,n]read only the first n objects

Reading complete binary files.

This writes out a 128-bit real number.
In[9]:=
Click for copyable input
Out[9]=
This reads back the bytes in the number.
In[10]:=
Click for copyable input
Out[10]=
This reads back the bytes as a sequence of 32-bit real numbers.
In[11]:=
Click for copyable input
Out[11]=
This treats the data as pairs containing a byte and a 32-bit real.
In[12]:=
Click for copyable input
Out[12]=

BinaryRead and BinaryWrite allow complete flexibility in reading and writing raw binary data. But in many practical applications one instead wants to work only with particular predefined formats. You can do this using Import and Export.

In addition to many complex formats, Import and Export support files containing sequences of identical data elements, of the same types as in BinaryRead and BinaryWrite. They also support the format, consisting of individual binary bits, represented as or .

New to Mathematica? Find your learning path »
Have a question? Ask support »