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
write objects of different types

Reading and writing binary data.

"Byte"8bit unsigned integer
"Character8"8bit character
"Character16"16bit character
"Complex64"IEEE singleprecision complex number
"Complex128"IEEE doubleprecision complex number
"Complex256"IEEE quadprecision complex number
"Integer8"8bit signed integer
"Integer16"16bit signed integer
"Integer32"32bit signed integer
"Integer64"64bit signed integer
"Integer128"128bit signed integer
"Real32"IEEE singleprecision real number
"Real64"IEEE doubleprecision real number
"Real128"IEEE quadprecision real number
"TerminatedString"nullterminated string of 8bit characters
"UnsignedInteger8"8bit unsigned integer
"UnsignedInteger16"16bit unsigned integer
"UnsignedInteger32"32bit unsigned integer
"UnsignedInteger64"64bit unsigned integer
"UnsignedInteger128"128bit unsigned integer

Types supported in BinaryRead and BinaryWrite.

This writes a sequence of bytes to a file:
Click for copyable input
BinaryWrite automatically opens a stream for the file. This closes it:
Click for copyable input
This reads the first byte from the file, returning it as an integer:
Click for copyable input
This reads the second 8 bits in the file as a character:
Click for copyable input
This reads the next 32 bits as a 32bit integer:
Click for copyable input

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 the Wolfram Language 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 32bit integer to a file:
Click for copyable input
This closes the file:
Click for copyable input
This reads the integer back, but assumes an opposite byte ordering:
Click for copyable input
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 128bit real number:
Click for copyable input
This reads back the bytes in the number:
Click for copyable input
This reads back the bytes as a sequence of 32bit real numbers:
Click for copyable input
This treats the data as pairs containing a byte and a 32bit real:
Click for copyable input

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 "Bit" format, consisting of individual binary bits, represented as 0 or 1.