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.
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 $ByteOrdering what byte ordering to use - BinaryWrite[channel,"string"] uses type "Character8", so all characters in "string" should have character codes in the range 0–255.
- BinaryWrite returns $Failed if it encounters a data element that cannot match the type specified.
Examples
open all close allBasic Examples (1)
Create a file with a sequence of bytes:
file = FileNameJoin[{$TemporaryDirectory, "test1"}];BinaryWrite[file, {8, 97, 255}];Close[file];Read the first byte from the file:
BinaryRead[file]BinaryRead[file]BinaryRead[file]Close[file];Scope (2)
Create a file containing three numbers in the "Integer16" format:
f = CreateFile[];
BinaryWrite[f, {10, 100, 1000}, "Integer16"];
Close[f];The write operation used two bytes for each value:
FileByteCount[f]In the common little-endian format, the values less than 128 are followed by a zero:
BinaryReadList[f]Read the file with a matching format:
BinaryReadList[f, "Integer16"]Rewrite the file using an optimal type for each value:
BinaryWrite[f, {10, 100, 1000}, {"Integer8", "Integer8", "Integer16"}];
Close[f];Now the file is only four bytes:
FileByteCount[f]It needs to be read in with a matching format:
BinaryReadList[f, {"Integer8", "Integer8", "Integer16"}]DeleteFile[f]Write bytes to the file specified using File object:
file = File[FileNameJoin[{$TemporaryDirectory, "test1"}]]BinaryWrite[file, {65, 66}];Close[file]BinaryReadList[file, {"Byte", "Byte"}]DeleteFile[file]Properties & Relations (1)
BinaryWrite can write ByteArray to files:
file = CreateFile[];ba = StringToByteArray["aαℵ"]BinaryWrite[file, ba];
Close[file]Use ReadByteArray to read it back in:
ba2 = ReadByteArray[file]ba == ba2Possible Issues (1)
By default, strings are interpreted as "Character8" and writing characters above 255 will fail:
file = CreateFile[];BinaryWrite[file, "α"]Specify "Character16" to allow all characters in the Unicode Basic Multilingual Plane:
BinaryWrite[file, "α", "Character16"]Use "Character32" to include any character:
BinaryWrite[file, "😂", "Character32"]Alternatively, convert strings to bytes in a given encoding using StringToByteArray:
BinaryWrite[file, StringToByteArray["😂"]]Close[file]See Also
BinaryRead Write WriteString Export BinarySerialize ByteArray
Formats: Byte
Function Repository: BinaryWriteAt
Tech Notes
History
Introduced in 2004 (5.1) | Updated in 2016 (11.0) ▪ 2019 (12.0)
Text
Wolfram Research (2004), BinaryWrite, Wolfram Language function, https://reference.wolfram.com/language/ref/BinaryWrite.html (updated 2019).
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
BibTeX
@misc{reference.wolfram_2026_binarywrite, author="Wolfram Research", title="{BinaryWrite}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/BinaryWrite.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_binarywrite, organization={Wolfram Research}, title={BinaryWrite}, year={2019}, url={https://reference.wolfram.com/language/ref/BinaryWrite.html}, note=[Accessed: 13-June-2026]}