簡體   English   中英

依次將類變量序列化為C#中的字節數組,反之亦然

[英]Serialize class variables sequentially to byte array in C# vice versa

我有一堂課,只是數字字段邏輯地編組。

我收到一些與這些類完全匹配的字節(字節的順序是變量的順序)。 我想將這些字節分配給這些類。

我可以編寫特定的序列化方法嗎?

舉例說明;

我有一個與Frame類完全匹配的順序byteArray,我的意思是;

-01-02-03-04-05-06 ....在框架類標題字段中, “ a”變量的值為0x01“ b”變量的值為0x02 ,然后為4個字節(c為Int32) 03-04-05 -06然后繼續使用val變量。

public class Frame
{
    public FrameHeader header;
    public FrameValue val;

}

public class FrameHeader
{
    public byte a;
    public byte b;
    public int c;
}

public class FrameValue
{
  public int x,y;
}

也許這個將byte []轉換為int的示例對您有幫助?

byte[] bytes = { 0x03, 0x04, 0x05, 0x06 };

// If the system architecture is little-endian (that is, little end first), reverse the byte array.
if (BitConverter.IsLittleEndian)
{
    Array.Reverse(bytes);
}

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM