({{, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,b]
writes a byte of data, specified as an integer from 0 to 255. }, {, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,{(Subscript[b, 1]),(Subscript[b, 2]),…}]
writes a sequence of bytes. }, {, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,"string"]
writes the raw sequence of characters in a string. }, {, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,(RefLink[ByteArray, paclet:ref/ByteArray])[…]]
writes the contents of a (RefLink[ByteArray, paclet:ref/ByteArray]) object.}, {, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,x,type]
writes an object of the specified type. }, {, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,{(Subscript[x, 1]),(Subscript[x, 2]),…},type]
writes a sequence of objects of the specified type. }, {, (RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,{(Subscript[x, 1]),(Subscript[x, 2]),…},{(Subscript[type, 1]),(Subscript[type, 2]),…}]
writes a sequence of objects with a sequence of types. }})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[BinaryWrite, paclet:ref/BinaryWrite]) supports the same types as (RefLink[BinaryRead, paclet:ref/BinaryRead]).

The output channel used by (RefLink[BinaryWrite, paclet:ref/BinaryWrite]) can be a single file or pipe, or a list of them, each specified as a string "name", as (RefLink[File, paclet:ref/File])["name"], or as an (RefLink[OutputStream, paclet:ref/OutputStream]) object that has been opened with (RefLink[BinaryFormat, paclet:ref/BinaryFormat])->(RefLink[True, paclet:ref/True]).

If any of the specified files or pipes are not already open, (RefLink[BinaryWrite, paclet:ref/BinaryWrite]) calls (RefLink[OpenWrite, paclet:ref/OpenWrite]) to open them.

(RefLink[BinaryWrite, paclet:ref/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:

(RefLink[BinaryWrite, paclet:ref/BinaryWrite])[channel,"string"] uses type "Character8", so all characters in "string" should have character codes in the range 0–255.

(RefLink[BinaryWrite, paclet:ref/BinaryWrite]) returns (RefLink[$Failed, paclet:ref/$Failed]) if it encounters a data element that cannot match the type specified.

OpenersPrimaryExamplesSection(Spacer1)Examples  (4)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Create a file with a sequence of bytes:

file =FileNameJoin[{ $TemporaryDirectory,"test1"}];

BinaryWrite[file, {8,97,255}];

Close the stream:

Close[file];

Read the first byte from the file:

BinaryRead[file]

BinaryRead[file]

BinaryRead[file]

Close the stream:

Close[file];

OpenersExampleSection1(Spacer1)Scope  (1)

Write bytes to the file specified using (RefLink[File, paclet:ref/File]) object:

file =File[FileNameJoin[{ $TemporaryDirectory,"test1"}]]

BinaryWrite[file,{65,66}];

Close[file]

BinaryReadList[file,{"Byte","Byte"}]

DeleteFile[file]

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

(RefLink[BinaryWrite, paclet:ref/BinaryWrite]) can write (RefLink[ByteArray, paclet:ref/ByteArray]) to files:

file=CreateFile[];

ba=StringToByteArray["aαℵ"]

BinaryWrite[file,ba];
Close[file]

Use (RefLink[ReadByteArray, paclet:ref/ReadByteArray]) to read it back in:

ba2=ReadByteArray[file]

ba==ba2

OpenersExampleSection4(Spacer1)Possible Issues  (1)

By default, strings are interpreted as (StringTypeLink["Character8", paclet:ref/format/Character8]) and writing characters above 255 will fail:

file=CreateFile[];

BinaryWrite[file,"α"]

Specify (StringTypeLink["Character16", paclet:ref/format/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 (RefLink[StringToByteArray, paclet:ref/StringToByteArray]):

BinaryWrite[file,StringToByteArray["😂"]]

Close[file]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[BinaryRead, paclet:ref/BinaryRead])  ▪  (RefLink[Write, paclet:ref/Write])  ▪  (RefLink[WriteString, paclet:ref/WriteString])  ▪  (RefLink[Export, paclet:ref/Export])  ▪  (RefLink[BinarySerialize, paclet:ref/BinarySerialize])  ▪  (RefLink[ByteArray, paclet:ref/ByteArray])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Binary Files, paclet:tutorial/BinaryFiles])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Binary Data, paclet:guide/BinaryData])}, {▪(RefLinkPlain[Network Programming, paclet:guide/NetworkProgramming])}, {▪(RefLinkPlain[Using Connected Devices, paclet:guide/UsingConnectedDevices])}, {▪(RefLinkPlain[Importing and Exporting, paclet:guide/ImportingAndExporting])}, {▪(RefLinkPlain[Files, paclet:guide/Files])}, {▪(RefLinkPlain[Signal Processing, paclet:guide/SignalProcessing])}, {▪(RefLinkPlain[Low-Level File Operations, paclet:guide/LowLevelFileOperations])}, {▪(RefLinkPlain[Direct Control of External Processes, paclet:guide/DirectControlOfExternalProcesses])}, {▪(RefLinkPlain[Systems-Related String Operations, paclet:guide/SystemsRelatedStringOperations])}})}})