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:
-
- Possible settings for PerformanceGoal include:
-
"Speed" optimize for serialization and deserialization speed"Size" optimize for smallness of serialized outputAutomatic automatically pick serialization strategy
- Possible settings for Method include:
-
{typespec1enc1,…} specify encodings for particular typesAutomatic 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)
Serialize an expression:
Serialize a graphic:
Convert the serialized form back to an expression:
Scope (7)
Apply BinarySerialize to symbolic expressions:
Serialize an image:
Note that the size of the serialized form roughly equals the byte count of the expression:
BinarySerialize efficiently stores machine integers:
BinarySerialize efficiently stores machine reals:
Compare with an arbitrary-precision real:
BinarySerialize efficiently stores strings:
BinarySerialize supports ByteArray :
BinarySerialize supports packed arrays:
Options (4)
Method (3)
Create a packed array of integer values:
By default, BinarySerialize uses the smallest integer type that fits the data:
Serialize the packed array using a bigger integer type:
Create a packed array of real values:
Serialize the array:
Serialize the array using machine floats, trading precision for a smaller output:
Create a packed array of complex values:
Serialize the array:
Serialize the array using lower precision:
PerformanceGoal (1)
Serialize a Dataset :
Serialize the same Dataset with PerformanceGoal set to "Size":
Both forms represent the same expression:
Applications (1)
Serialize a trained neural network:
Create a new file:
Open an output binary stream:
Write the serialized expression to the file:
Read the data from the file:
Plot the prediction of the net as a function of the input:
Properties & Relations (4)
Using PerformanceGoal->"Speed" is faster but produces larger outputs:
Using PerformanceGoal->"Size" is slower but produces smaller outputs:
BinarySerialize encodes strings as UTF-8:
BinaryDeserialize is the inverse of BinarySerialize :
DumpSave stores definitions attached to a symbol in a file:
Using Get will return Null but restore the value of f:
BinarySerialize only stores the input expression, which can be restored by BinaryDeserialize :
Possible Issues (2)
From small expressions, PerformanceGoal->"Size" may not produce smaller outputs:
The Method parameter "PackedArrayIntegerType" must be chosen consistently with the data:
These values require 32-bit integers:
A setting of Automatic will always use a size that fits the data:
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_2025_binaryserialize, author="Wolfram Research", title="{BinarySerialize}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/BinarySerialize.html}", note=[Accessed: 05-December-2025]}
BibLaTeX
@online{reference.wolfram_2025_binaryserialize, organization={Wolfram Research}, title={BinarySerialize}, year={2018}, url={https://reference.wolfram.com/language/ref/BinarySerialize.html}, note=[Accessed: 05-December-2025]}