BinarySerialize[expr]
用 ByteArray 对象给出任何表达式 expr 的二进制表示.
BinarySerialize
BinarySerialize[expr]
用 ByteArray 对象给出任何表达式 expr 的二进制表示.
更多信息和选项
- BinarySerialize 有以下选项:
-
Method Automatic 要使用的序列化方法的详细信息 PerformanceGoal Automatic 尝试优化性能方面 - PerformanceGoal 的可能设置包括:
-
"Speed" 优化用于序列化和反序列化速度 "Size" 序列输出的小型化的优化 Automatic 自动选取序列化策略 - Method 的可能设置包括:
-
{typespec1enc1,…} 为特殊类型指定编码 Automatic 基于数据自动选取编码 - typespeci 的可能格式包括:
-
"PackedArrayIntegerType" 整数打包数组 "PackedArrayRealType" 实数值打包数组 "PackedArrayComplexType" 复数值打包数组 - "PackedArrayIntegerType" 的可能设置包括 "Integer8"、"Integer16"、"Integer32" 和 "Integer64"(仅用于 64 位系统).
- "PackedArrayRealType" 的可能设置包括 "Real32" 和 "Real64".
- "PackedArrayComplexType" 的可能设置包括 "Complex64" 和 "Complex128".
- enci 编码规范的默认设置是 Automatic,这时 BinarySerialize 会使用适于给定数据的类型.
- BinarySerialize 使用 WXF 格式.
- BinaryDeserialize 是 BinarySerialize 的反函数.
范例
打开所有单元 关闭所有单元基本范例 (2)
范围 (7)
把 BinarySerialize 应用于符号表达式:
BinarySerialize[f[Sin[g[x]]]]BinarySerialize[[image]]ByteCount[[image]]BinarySerialize 有效地存储机器整数:
BinarySerialize[2 ^ 7 - 1]BinarySerialize[2 ^ 15 - 1]BinarySerialize[2 ^ 31 - 1]BinarySerialize 有效地存储机器实数:
BinarySerialize[N[1234 / 5678]]BinarySerialize[N[1234 / 5678, 16]]BinarySerialize 有效存储字符串:
str = FromCharacterCode[RandomInteger[{0, 2 ^ 15}, 1000]];ByteCount[str]BinarySerialize[str]BinarySerialize[ByteArray[RandomInteger[255, 1000]]]BinarySerialize 支持打包数组:
ints = RandomInteger[2 ^ ($SystemWordLength - 2), 1000];
reals = RandomReal[1, 1000];
complexes = RandomComplex[{0, 1 + I}, 1000];ByteCount /@ {ints, reals, complexes}BinarySerialize /@ {ints, reals, complexes}选项 (4)
Method (3)
integers = ConstantArray[2 ^ 14, 3];默认情况下,BinarySerialize 使用适于数据的最小整数类型:
BinarySerialize[integers]BinarySerialize[integers, Method -> "PackedArrayIntegerType" -> "Integer32"]reals = RandomReal[1, 3]real64 = BinarySerialize[reals]real32 = BinarySerialize[reals, Method -> "PackedArrayRealType" -> "Real32"]complexes = RandomComplex[1 + I, 3]BinarySerialize[complexes]BinarySerialize[complexes, Method -> {"PackedArrayComplexType" -> "Complex64"}]PerformanceGoal (1)
序列化一个 Dataset:
data = ExampleData[{"NetworkGraph", "AstrophysicsCollaborations"}];uncompressed = BinarySerialize[data]序列化同样的 Dataset, PerformanceGoal 设为 "Size":
compressed = BinarySerialize[data, PerformanceGoal -> "Size"]BinaryDeserialize[uncompressed] == BinaryDeserialize[compressed]应用 (1)
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]BinaryWrite[ow, bin]Close[ow];nettrained = BinaryDeserialize[ByteArray[BinaryReadList[file, "Byte"]]]Plot[nettrained[x], {x, 0, 5}]属性和关系 (4)
使用 PerformanceGoal->"Speed" 更快,但产生更大的输出:
data = Range[10 ^ 6];BinarySerialize[data, PerformanceGoal -> "Speed"]//AbsoluteTiming使用 PerformanceGoal->"Size" 更慢,但产生更小的输出:
BinarySerialize[data, PerformanceGoal -> "Size"]//AbsoluteTimingBinarySerialize 把字符串编码为 UTF-8:
bytes = BinarySerialize["→ hello there ←"]ByteArrayToString[bytes, "UTF-8"]BinaryDeserialize 是 BinarySerialize 的反函数:
BinaryDeserialize[BinarySerialize[x]] === xDumpSave 存储附加在文件中符号的定义:
f[x_] := x ^ 2mx = FileNameJoin[{$TemporaryDirectory, "f.mx"}];
DumpSave[mx, f];Clear[f];
restored = Get[mx];
DeleteFile[mx];
{restored, f[x]}BinarySerialize 只存储输入表达式,可以用 BinaryDeserialize 恢复:
BinarySerialize[f];
Clear[f];
restored = BinaryDeserialize[%%];
{restored, f[x]}可能存在的问题 (2)
对于小型表达式,PerformanceGoal->"Size" 可能不能产生更小的输出:
BinarySerialize[{1, 2, 3}, PerformanceGoal -> "Size"]BinarySerialize[{1, 2, 3}]Method 参数 "PackedArrayIntegerType" 必须与数据选择一致:
BinarySerialize[ConstantArray[2 ^ 17, 3], Method -> {"PackedArrayIntegerType" -> "Integer16"}]BinarySerialize[ConstantArray[2 ^ 17, 3], Method -> {"PackedArrayIntegerType" -> "Integer32"}]Automatic 的设置将总是使用适于数据的尺寸:
BinarySerialize[ConstantArray[2 ^ 17, 3], Method -> {"PackedArrayIntegerType" -> Automatic}]参见
BinaryDeserialize DumpSave Compress ExportByteArray ByteArray BaseEncode ByteArrayToString StringToByteArray Export FullForm InputForm BinaryWrite
编译类型: PackedArray
格式: WXF
Function Repository: BinarySerializeWithDefinitions
技术笔记
-
▪
- WXF 格式简介
相关指南
-
▪
- 文件中的 Wolfram 语言表达式 ▪
- 二进制数据 ▪
- 导入和导出数据库格式 ▪
- 导入和导出 ▪
- 内存测量和优化 ▪
- 文件 ▪
- Wolfram 语言文件格式
文本
Wolfram Research (2017),BinarySerialize,Wolfram 语言函数,https://reference.wolfram.com/language/ref/BinarySerialize.html (更新于 2018 年).
CMS
Wolfram 语言. 2017. "BinarySerialize." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2018. https://reference.wolfram.com/language/ref/BinarySerialize.html.
APA
Wolfram 语言. (2017). BinarySerialize. Wolfram 语言与系统参考资料中心. 追溯自 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: 18-August-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: 18-August-2026]}