MATHEMATICA TUTORIAL

# 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

 "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.
 Out[1]=
BinaryWrite automatically opens a stream for the file. This closes it.
This reads the first byte from the file, returning it as an integer.
 Out[3]=
This reads the second 8 bits in the file as a character.
 Out[4]=
This reads the next 32 bits as a 32-bit integer.
 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 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.
 Out[6]=
This closes the file.
This reads the integer back, but assumes an opposite byte ordering.
 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

This writes out a 128-bit real number.
 Out[9]=
This reads back the bytes in the number.
 Out[10]=
This reads back the bytes as a sequence of 32-bit real numbers.
 Out[11]=
This treats the data as pairs containing a byte and a 32-bit real.
 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 .

## Tutorial CollectionTutorial Collection

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