Binary Files

Functions like (RefLink[Read, paclet:ref/Read]) and (RefLink[Write, paclet:ref/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 (RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/BinaryWrite]).

Reading and writing binary data.

Types supported in (RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/BinaryWrite]).

This writes a sequence of bytes to a file:

BinaryWrite["tmp",{97,98,99,100,101}]

(RefLink[BinaryWrite, paclet:ref/BinaryWrite]) automatically opens a stream for the file. This closes it:

Close["tmp"];

This reads the first byte from the file, returning it as an integer:

BinaryRead["tmp"]

This reads the second 8 bits in the file as a character:

BinaryRead["tmp","Character8"]

This reads the next 32 bits as a 32-bit integer:

BinaryRead["tmp","Integer32"]

Like (RefLink[Read, paclet:ref/Read]) and (RefLink[Write, paclet:ref/Write]), (RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/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 (RefLink[OpenRead, paclet:ref/OpenRead]) or (RefLink[OpenWrite, paclet:ref/OpenWrite]). On some computer systems, the option setting (RefLink[BinaryFormat, paclet:ref/BinaryFormat])->(RefLink[True, paclet:ref/True]) is required for any stream to be used with (RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/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 (RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/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 (RefLink[$ByteOrdering, paclet:ref/$ByteOrdering]).

This writes a 32-bit integer to a file:

BinaryWrite["tmp2",45671,"Integer32"]

This closes the file:

Close["tmp2"];

This reads the integer back, but assumes an opposite byte ordering:

BinaryRead["tmp2","Integer32",ByteOrdering->-$ByteOrdering]

Reading complete binary files.

This writes out a 128-bit real number:

BinaryWrite["tmp3",5.67891,"Real128"]

This reads back the bytes in the number:

BinaryReadList["tmp3","Byte"]

This reads back the bytes as a sequence of 32-bit real numbers:

BinaryReadList["tmp3","Real32"]

This treats the data as pairs containing a byte and a 32-bit real:

BinaryReadList["tmp3",{"Byte","Real32"}]

(RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/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 (RefLink[Import, paclet:ref/Import]) and (RefLink[Export, paclet:ref/Export]).

In addition to many complex formats, (RefLink[Import, paclet:ref/Import]) and (RefLink[Export, paclet:ref/Export]) support files containing sequences of identical data elements, of the same types as in (RefLink[BinaryRead, paclet:ref/BinaryRead]) and (RefLink[BinaryWrite, paclet:ref/BinaryWrite]). They also support the "Bit" format, consisting of individual binary bits, represented as 0 or 1.