C#相当于Python的struct.pack的 [英] C# equivalent of python's struct.pack
问题描述
是否有C#库,允许类似的功能Python的结构
从的标准库?
Is there a library for C# that allows similar functionality to python's struct
from the standard library?
一个可以与真正的对准结构颇有渊源模拟结构库。但我没有找到还没有任何办法直接控制在C#中的字节序的结构(C#中的结构似乎更适合于COM互操作,并减少对通用二进制包装)。
One can emulate the struct library quite closely with real aligned structs. But I didn't find yet any way to directly control the endianess in C#'s structs (the C#'s structs seems to be geared more towards COM interop, and less toward general purpose binary packing).
推荐答案
该最接近的很可能是使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx\"><$c$c>BinaryWriter$c$c>写入一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx\"><$c$c>MemoryStream$c$c>,或<一个href=\"http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx\"><$c$c>BitConverter$c$c>对于一次性转换单个值到字节数组中。
The closest equivalent would probably be to use BinaryWriter
writing into a MemoryStream
, or BitConverter
for a one-off conversion of a single value into a byte array.
如果你想控制的字节顺序,可以使用来自 MiscUtil 我EndianBinaryWriter / EndianBitConverter。一个额外的功能,我的 EndianBitConverter
的是,你可以转换到一个现有的阵列,这将帮助你,如果你想端口结构的特定部分。包
自己。
If you want to control the endianness of that, you can use my EndianBinaryWriter/EndianBitConverter from MiscUtil. One extra feature of my EndianBitConverter
is that you can convert into a specific portion of an existing array, which would help you if you wanted to port struct.pack
yourself.
这篇关于C#相当于Python的struct.pack的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!