binaryfiles - Is there an equivalent in C# to pack and unpack from Perl? -
is there library permit same pack , unpack perl function?
with template list http://perldoc.perl.org/functions/pack.html,
pack converting list binary representation
unpack converting binary structure normal perl variables
to brief:
i give byte[] array, template parse packet, , variable receive extracted data.
it seems mono providing such feature, templates not same, see http://www.mono-project.com/mono_dataconvert#obtaining_mono.dataconvert.
it seems want know binary serialization. link;
serialization can defined process of storing state of object storage medium. during process, the public , private fields of object , name of class, including assembly containing class, are converted stream of bytes, written data stream. when object subsequently deserialized, exact clone of original object created.
a more concrete example of binary serialization c# (targeting .net framework 4.5) can found here. brief synopsis; must annotate class wish serialize , deserialize [serializable] tag, , use formatter instance serialization/deserialization.
so in perl can say:
pack template,list
in c# you'll need following:
[serializable] public class myobject { public int n1 = 0; public int n2 = 0; public string str = null; } // ... , in other class have application logic public void pack() { myobject obj = new myobject(); obj.n1 = 1; obj.n2 = 24; obj.str = "some string"; iformatter formatter = new binaryformatter(); stream stream = new filestream("myfile.bin", filemode.create, fileaccess.write, fileshare.none); formatter.serialize(stream, obj); stream.close(); }
to address idea going want control serialization template, you'll need implement iserializable yourself. here's msdn article custom binary serialization. implementing interface yourself, gain high amount of control of binary template in exchange high amount of complexity in ensuring functionality.
Comments
Post a Comment