BinaryWrite

BinaryWrite[channel,b]

writes a byte of data, specified as an integer from 0 to 255.

BinaryWrite[channel,{b1,b2,}]

writes a sequence of bytes.

BinaryWrite[channel,"string"]

writes the raw sequence of characters in a string.

BinaryWrite[channel,ByteArray[]]

writes the contents of a ByteArray object.

BinaryWrite[channel,x,type]

writes an object of the specified type.

BinaryWrite[channel,{x1,x2,},type]

writes a sequence of objects of the specified type.

BinaryWrite[channel,{x1,x2,},{type1,type2,}]

writes a sequence of objects with a sequence of types.

Details and Options

  • BinaryWrite supports the same types as BinaryRead.
  • The output channel used by BinaryWrite can be a single file or pipe, or a list of them, each specified as a string "name", as File["name"], or as an OutputStream object that has been opened with BinaryFormat->True.
  • If any of the specified files or pipes are not already open, BinaryWrite calls OpenWrite to open them.
  • BinaryWrite does not close files and pipes after it finishes writing to them.
  • When a list of types is given, the list is effectively repeated as many times as necessary.
  • The following options can be given:
  • ByteOrdering$ByteOrderingwhat byte ordering to use
  • BinaryWrite[channel,"string"] uses type "Character8", so all characters in "string" should have character codes in the range 0255.
  • BinaryWrite returns $Failed if it encounters a data element that cannot match the type specified.

Examples

open allclose all

Basic Examples  (1)

Create a file with a sequence of bytes:

Close the stream:

Read the first byte from the file:

Close the stream:

Scope  (1)

Write bytes to the file specified using File object:

Properties & Relations  (1)

BinaryWrite can write ByteArray to files:

Use ReadByteArray to read it back in:

Possible Issues  (1)

By default, strings are interpreted as "Character8" and writing characters above 255 will fail:

Specify "Character16" to allow all characters in the Unicode Basic Multilingual Plane:

Use "Character32" to include any character:

Alternatively, convert strings to bytes in a given encoding using StringToByteArray:

Wolfram Research (2004), BinaryWrite, Wolfram Language function, https://reference.wolfram.com/language/ref/BinaryWrite.html (updated 2019).

Text

Wolfram Research (2004), BinaryWrite, Wolfram Language function, https://reference.wolfram.com/language/ref/BinaryWrite.html (updated 2019).

BibTeX

@misc{reference.wolfram_2020_binarywrite, author="Wolfram Research", title="{BinaryWrite}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/BinaryWrite.html}", note=[Accessed: 03-December-2020 ]}

BibLaTeX

@online{reference.wolfram_2020_binarywrite, organization={Wolfram Research}, title={BinaryWrite}, year={2019}, url={https://reference.wolfram.com/language/ref/BinaryWrite.html}, note=[Accessed: 03-December-2020 ]}

CMS

Wolfram Language. 2004. "BinaryWrite." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2019. https://reference.wolfram.com/language/ref/BinaryWrite.html.

APA

Wolfram Language. (2004). BinaryWrite. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/BinaryWrite.html