BinaryRead

BinaryRead[stream]
reads one byte of raw binary data from an input stream, and returns an integer from 0 to 255.

BinaryRead[stream,type]
reads an object of the specified type.

BinaryRead[stream,{type1,type2,}]
reads a sequence of objects of the specified types.

Details and OptionsDetails and Options

  • Possible types to read are:
  • "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
    "Integer24"24-bit 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
    "UnsignedInteger24"24-bit unsigned integer
    "UnsignedInteger32"32bit unsigned integer
    "UnsignedInteger64"64bit unsigned integer
    "UnsignedInteger128"128bit unsigned integer
  • The first argument to BinaryRead can be InputStream["name",n], or simply if there is only one open input stream with the specified name.
  • You can open a file or pipe to get an InputStream object using OpenRead.
  • Streams for use with BinaryRead should be opened with BinaryFormat->True.
  • There is always a "current point" maintained for any stream. When you read an object from a stream, the current point is left after the input you read. Successive calls to BinaryRead can therefore be used to read successive objects in a stream such as a file.
  • BinaryRead returns EndOfFile if you are at the end of the file.
  • BinaryRead returns Infinity for IEEE "infinity", and Indeterminate for IEEE "notanumber".
  • The following options can be given:
  • ByteOrdering$ByteOrderingwhat byte ordering to use
    Path$Paththe path to search for files to be opened

ExamplesExamplesopen allclose all

Basic Examples  (1)Basic Examples  (1)

Create a file with a sequence of bytes:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input

Read the first byte from the file:

In[4]:=
Click for copyable input
Out[4]=

Read the second byte as an 8-bit character:

In[5]:=
Click for copyable input
Out[5]=

Read the next byte as an 8-bit signed integer:

In[6]:=
Click for copyable input
Out[6]=

Close the file:

In[7]:=
Click for copyable input
Out[7]=
Introduced in 2004
(5.1)
| Updated in 2007
(6.0)