Is there an equivalent in C # for packing and unpacking from Perl?

Is there a library that allows you to do the same thing as the pack and unpack Perl functions?

In the template list http://perldoc.perl.org/functions/pack.html ,

pack is designed to convert the list to binary representation
unpack - convert binary structure to regular Perl variables

To be brief:

I give an array of byte [], a template for parsing the package, and a variable that will receive the extracted data.

Mono seems to provide such a function, but the templates do not match, see http://www.mono-project.com/Mono_DataConvert#Obtaining_Mono.DataConvert .

+4
source share
2 answers

It seems that you want to know about Binary Serialization . From the link:

Serialization can be defined as the process of storing the state of an object in the medium store. During this process, the public and private fields of the object and the class name , including the assembly containing the class, are converted to a byte stream , which is then written to the data stream. When an object is subsequently deserialized, an exact clone of the original object is created.

A more specific example of Binary Serialization for C # (oriented to the .NET Framework 4.5) can be found here . Short review; you must annotate the class you want to serialize and deserialize using the [Serializable] tag, and then use the Formatter instance to actually serialize / deserialize.

So, in Perl, where you can just say:

pack TEMPLATE,LIST 

In C # you will need the following:

 [Serializable] public class MyObject { public int n1 = 0; public int n2 = 0; public String str = null; } // ... And in some other class where you have you 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 solve the idea that you want to manage a serialization pattern, you will most likely have to implement ISerializable yourself. Here is an MSDN article on custom binary serialization . By implementing the interface yourself, you get a very high level of binary template management in exchange for high complexity in providing functionality.

0
source

http://msdn.microsoft.com/en-us/library/system.bitconverter.tosingle%28v=vs.110%29.aspx

The link above shows methods that are more directly equivalent to package and unpacking.

-one
source

Source: https://habr.com/ru/post/1495910/


All Articles