Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Previous section-----Next section

2.12.11 Advanced Topic: 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, { ,  , ... }]
read a list of objects
BinaryWrite[stream, b] write one byte
BinaryWrite[stream, { ,  , ... }] 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, { ,  , ... }, type]
write a sequence of objects
BinaryWrite[stream, { ,  , ... }, { ,  , ... }]
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]:=  BinaryWrite["tmp", {97,98,99,100,101}]

Out[1]=

BinaryWrite automatically opens a stream for the file. This closes it.

In[2]:=  Close["tmp"];

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

In[3]:=  BinaryRead["tmp"]

Out[3]=

This reads the second 8 bits in the file as character.

In[4]:=  BinaryRead["tmp", "Character8"]

Out[4]=

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

In[5]:=  BinaryRead["tmp", "Integer32"]

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 write a 32-bit integer to a file.

In[6]:=  BinaryWrite["tmp2", 45671, "Integer32"]

Out[6]=

This closes the file.

In[7]:=  Close["tmp2"];

This reads the integer back, but assuming an opposite byte ordering.

In[8]:=  BinaryRead["tmp2", "Integer32",
ByteOrdering -> -$ByteOrdering]

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", { ,  , ... }]
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]:=  BinaryWrite["tmp3", 5.67891, "Real128"]

Out[9]=

This reads back the bytes in the number.

In[10]:=  BinaryReadList["tmp3", "Byte"]

Out[10]=

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

In[11]:=  BinaryReadList["tmp3", "Real32"]

Out[11]=

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

In[12]:=  BinaryReadList["tmp3", {"Byte", "Real32"}]

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


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: