BinarySerialize[expr]
gives a binary representation of any expression expr as a ByteArray object.
BinarySerialize
BinarySerialize[expr]
gives a binary representation of any expression expr as a ByteArray object.
Details and Options
- BinarySerialize has the following options:
-
Method Automatic details of serialization methods to use PerformanceGoal Automatic aspects of performance to try to optimize - Possible settings for PerformanceGoal include:
-
"Speed" optimize for serialization and deserialization speed "Size" optimize for smallness of serialized output Automatic automatically pick serialization strategy - Possible settings for Method include:
-
{typespec1enc1,…} specify encodings for particular types Automatic pick encodings automatically based on data - Possible forms for the typespeci include:
-
"PackedArrayIntegerType" integer packed arrays "PackedArrayRealType" real-valued packed arrays "PackedArrayComplexType" complex-valued packed arrays - Possible settings for "PackedArrayIntegerType" include "Integer8", "Integer16", "Integer32" and "Integer64" (64-bit systems only).
- Possible settings for "PackedArrayRealType" include "Real32" and "Real64".
- Possible settings for "PackedArrayComplexType" include "Complex64" and "Complex128".
- The default for the enci encoding specifications is Automatic, in which case BinarySerialize will use a type that fits the data it is given.
- BinarySerialize uses the WXF format.
- BinaryDeserialize is the inverse of BinarySerialize.
Examples
open all close allBasic Examples (2)
Scope (7)
Apply BinarySerialize to symbolic expressions:
BinarySerialize[f[Sin[g[x]]]]BinarySerialize[[image]]Note that the size of the serialized form roughly equals the byte count of the expression:
ByteCount[[image]]BinarySerialize efficiently stores machine integers:
BinarySerialize[2 ^ 7 - 1]BinarySerialize[2 ^ 15 - 1]BinarySerialize[2 ^ 31 - 1]BinarySerialize efficiently stores machine reals:
BinarySerialize[N[1234 / 5678]]Compare with an arbitrary-precision real:
BinarySerialize[N[1234 / 5678, 16]]BinarySerialize efficiently stores strings:
str = FromCharacterCode[RandomInteger[{0, 2 ^ 15}, 1000]];ByteCount[str]BinarySerialize[str]BinarySerialize supports ByteArray:
BinarySerialize[ByteArray[RandomInteger[255, 1000]]]BinarySerialize supports packed arrays:
ints = RandomInteger[2 ^ ($SystemWordLength - 2), 1000];
reals = RandomReal[1, 1000];
complexes = RandomComplex[{0, 1 + I}, 1000];ByteCount /@ {ints, reals, complexes}BinarySerialize /@ {ints, reals, complexes}Options (4)
Method (3)
Create a packed array of integer values:
integers = ConstantArray[2 ^ 14, 3];By default, BinarySerialize uses the smallest integer type that fits the data:
BinarySerialize[integers]Serialize the packed array using a bigger integer type:
BinarySerialize[integers, Method -> "PackedArrayIntegerType" -> "Integer32"]Create a packed array of real values:
reals = RandomReal[1, 3]real64 = BinarySerialize[reals]Serialize the array using machine floats, trading precision for a smaller output:
real32 = BinarySerialize[reals, Method -> "PackedArrayRealType" -> "Real32"]Create a packed array of complex values:
complexes = RandomComplex[1 + I, 3]BinarySerialize[complexes]Serialize the array using lower precision:
BinarySerialize[complexes, Method -> {"PackedArrayComplexType" -> "Complex64"}]PerformanceGoal (1)
Serialize a Dataset:
data = ExampleData[{"NetworkGraph", "AstrophysicsCollaborations"}];uncompressed = BinarySerialize[data]Serialize the same Dataset with PerformanceGoal set to "Size":
compressed = BinarySerialize[data, PerformanceGoal -> "Size"]Both forms represent the same expression:
BinaryDeserialize[uncompressed] == BinaryDeserialize[compressed]Applications (1)
Serialize a trained neural network:
bin = BinarySerialize[NetTrain[NetGraph[{LinearLayer[1], LinearLayer[3], LinearLayer[3], LinearLayer["Real"]}, {1 -> 2 -> 3 -> 4}, "Input" -> "Real"], {.1 -> .07, .25 -> .24, .3 -> .32, .5 -> .5}, MaxTrainingRounds -> 1000]]file = CreateFile[];ow = OpenWrite[file, BinaryFormat -> True]Write the serialized expression to the file:
BinaryWrite[ow, bin]Close[ow];nettrained = BinaryDeserialize[ByteArray[BinaryReadList[file, "Byte"]]]Plot the prediction of the net as a function of the input:
Plot[nettrained[x], {x, 0, 5}]Properties & Relations (4)
Using PerformanceGoal->"Speed" is faster but produces larger outputs:
data = Range[10 ^ 6];BinarySerialize[data, PerformanceGoal -> "Speed"]//AbsoluteTimingUsing PerformanceGoal->"Size" is slower but produces smaller outputs:
BinarySerialize[data, PerformanceGoal -> "Size"]//AbsoluteTimingBinarySerialize encodes strings as UTF-8:
bytes = BinarySerialize["→ hello there ←"]ByteArrayToString[bytes, "UTF-8"]BinaryDeserialize is the inverse of BinarySerialize:
BinaryDeserialize[BinarySerialize[x]] === xDumpSave stores definitions attached to a symbol in a file:
f[x_] := x ^ 2mx = FileNameJoin[{$TemporaryDirectory, "f.mx"}];
DumpSave[mx, f];Using Get will return Null but restore the value of f:
Clear[f];
restored = Get[mx];
DeleteFile[mx];
{restored, f[x]}BinarySerialize only stores the input expression, which can be restored by BinaryDeserialize:
BinarySerialize[f];
Clear[f];
restored = BinaryDeserialize[%%];
{restored, f[x]}Possible Issues (2)
From small expressions, PerformanceGoal->"Size" may not produce smaller outputs:
BinarySerialize[{1, 2, 3}, PerformanceGoal -> "Size"]BinarySerialize[{1, 2, 3}]The Method parameter "PackedArrayIntegerType" must be chosen consistently with the data:
BinarySerialize[ConstantArray[2 ^ 17, 3], Method -> {"PackedArrayIntegerType" -> "Integer16"}]These values require 32-bit integers:
BinarySerialize[ConstantArray[2 ^ 17, 3], Method -> {"PackedArrayIntegerType" -> "Integer32"}]A setting of Automatic will always use a size that fits the data:
BinarySerialize[ConstantArray[2 ^ 17, 3], Method -> {"PackedArrayIntegerType" -> Automatic}]See Also
BinaryDeserialize DumpSave Compress ExportByteArray ByteArray BaseEncode ByteArrayToString StringToByteArray Export FullForm InputForm BinaryWrite
Compiled Types: PackedArray
Formats: WXF
Function Repository: BinarySerializeWithDefinitions
Tech Notes
Text
Wolfram Research (2017), BinarySerialize, Wolfram Language function, https://reference.wolfram.com/language/ref/BinarySerialize.html (updated 2018).
CMS
Wolfram Language. 2017. "BinarySerialize." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2018. https://reference.wolfram.com/language/ref/BinarySerialize.html.
APA
Wolfram Language. (2017). BinarySerialize. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/BinarySerialize.html
BibTeX
@misc{reference.wolfram_2026_binaryserialize, author="Wolfram Research", title="{BinarySerialize}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/BinarySerialize.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_binaryserialize, organization={Wolfram Research}, title={BinarySerialize}, year={2018}, url={https://reference.wolfram.com/language/ref/BinarySerialize.html}, note=[Accessed: 13-June-2026]}