convert dictionary or list to byte[]

后端 未结 2 1054
慢半拍i
慢半拍i 2020-12-13 21:39

Ok, i\'ve seen many similar questions both on here and unity forums asking about converting from one format to another. I\'ve got a (hopefully) simple question that i just c

相关标签:
2条回答
  • 2020-12-13 22:31

    You may want to try serialization.

    var binFormatter = new BinaryFormatter();
    var mStream = new MemoryStream();
    binFormatter.Serialize(mStream, myObjToSerialize);
    
    //This gives you the byte array.
    mStream.ToArray();
    

    And then if you want to turn the byte array back into an object:

    var mStream = new MemoryStream();
    var binFormatter = new BinaryFormatter();
    
    // Where 'objectBytes' is your byte array.
    mStream.Write (objectBytes, 0, objectBytes.Length);
    mStream.Position = 0;
    
    var myObject = binFormatter.Deserialize(mStream) as YourObjectType;
    
    0 讨论(0)
  • 2020-12-13 22:32

    Converting data into byte stream (and back) is called serialization (and deserialization).

    You can use the BinaryFormatter class to do so.

    0 讨论(0)
提交回复
热议问题