This article will tell you the basic binary processing knowledge in dotnet, and the simple method of converting structure arrays and binary arrays into C#.
Although this article is a basic introductory knowledge, please understand the structure memory layout knowledge in C# before reading it.
This article will introduce to you the MemoryMarshal auxiliary class, which uses this auxiliary class to realize the mutual conversion of structure arrays and binary arrays.
First, we demonstrate how to convert each other from structure arrays and binary arrays. To be precise, it is the mutual conversion between Spans, rather than really converting them into arrays, but the behavior of Span is very similar to that of arrays.
To facilitate code demonstration, I have defined a Foo1 structure. All the codes in this article can be found at the end of this article.
struct Foo1 { public int A { get; set; } public int B { get; set; } public int C { get; set; } }
First create a Foo1 structure array. For the sake of convenience, I also assign values to each attribute of Foo1, as shown in the following code
var foo1 = new Foo1() { A = 1, B = 2, C = 3, }; var foo1Array = new Foo1[] { foo1 };
After getting the array of Foo1, it can be converted to Span type very conveniently, and you only need to call it()
Just do it. Next, convert the Foo1 array into a binary array, to be preciseSpan<byte>
Type, code as follows
Span<byte> foo1ByteSpan = (());
Now write an auxiliary method andfoo1ByteSpan
The content of is output to the console to facilitate everyone to see thisfoo1ByteSpan
The object is indeed the binary content of the memory space of the Foo1 structure
Log(foo1ByteSpan); // 01 00 00 00 02 00 00 00 03 00 00 00 private static void Log(Span<byte> byteSpan) { var stringBuilder = new StringBuilder(); foreach (var b in byteSpan) { (("X2")); (' '); } (()); }
You can see that the above output 01 02 03 is the values corresponding to the A, B and C attributes of the Foo1 structure. This article does not make a fixed layout of the Foo1 structure, etc., which is not rigorous enough. In other words, I can only guarantee with you the values of the A, B and C attributes of the Foo1 structure, but the order in which these values appear cannot be guaranteed. If you don't understand this part of the knowledge, please check the memory layout optimization and memory alignment of the structure in dotnet
Next, start to prove what you have obtained above in this articlefoo1ByteSpan
andfoo1Array
Point to the same memory address space, that is,foo1Array
orfoo1ByteSpan
The memory modifications will affect each other
Modify firstfoo1Array
The content inside, such as modifying the content of a property, such as the following code
foo1Array[0].C = 5; Log(foo1ByteSpan); // 01 00 00 00 02 00 00 00 05 00 00 00
You can see the printed out after modifying the C attributefoo1ByteSpan
Also changed
Try to modify it againfoo1ByteSpan
, see if the content can also affect thefoo1Array
Object
foo1ByteSpan[0] = 6; (foo1Array[0].A); // 6 var foo1Span = <byte, Foo1>(foo1ByteSpan); (foo1Span[0].A); // 6
It can be proved by the above code.foo1ByteSpan
andfoo1Array
Point to the same memory address space, that is, it is not a reapplying memory space to store the array content, but just magic on the code written, and the memory is the same space. This reduces memory space conversion and copy, greatly improves performance, and also takes into account security.
Through the method, you can not only support the conversion between the structure and byte, but also perform conversion between the structures. For example, define a Foo2 type. This Foo2 type and Foo1 type have the same attributes but the types are different. Try using the following code to convert each other
var foo2Span = <Foo1, Foo2>(foo1Span); (foo2Span[0].A); // 6 (foo2Span[0].B); // 2 (foo2Span[0].C); // 5 struct Foo2 { public int A { get; set; } public int B { get; set; } public int C { get; set; } }
It can be seen that direct conversion between multiple structures can be achieved through , and the array space is not reopened on the heap
However, the above code in this article is not rigorous. The above code does not fix the memory layout of the Foo1 structure and the Foo2 structure. The above code is only used to tell everyone how to use it, rather than recommending everyone to follow me in formal projects. If you are in a formal project, you need to ensure that the memory layout between multiple structures is the same or that the direct memory conversion in each case is in line with expectations.
The code in this article is placed ingithubandgiteeWelcome to visit
You can obtain the source code of this article by creating an empty folder first, then use the command line cd command to enter this empty folder, enter the following code in the command line to obtain the code of this article
git init git remote add origin /lindexi/lindexi_gd.git git pull origin 6bd28ceca1e9b73bfda270f9a3a3bddd7b8ebcc4
The above is the source of gitee. If gitee cannot be accessed, please replace it with the source of github. Please continue to enter the following code on the command line
git remote remove origin git remote add origin /lindexi/lindexi_gd.git git pull origin 6bd28ceca1e9b73bfda270f9a3a3bddd7b8ebcc4
After obtaining the code, enter the HallehuwearjewhoQedelqarnalar folder
This is the end of this article about the mutual conversion of C# binary arrays and structures. For more related contents of C# binary arrays and structures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!