110,006
社区成员




// 其他代码在9楼
public byte[] GetSerializedBytes(byte[] header)
{
BinaryFormatter bf = new BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
// 如果需要,这里可以加入包头
ms.Write( header, 0, header.Length);
bf.Serialize(ms, this); //<---系列化成byte数组
ms.Seek(0, System.IO.SeekOrigin.Begin);
return ms.ToArray();
}
}
public static Astruct FromSerilizedBytes(byte[] data, byte[] header)
{
BinaryFormatter bf = new BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
// 如果需要,这里可以拆出包头
ms.Read(header, 0, header.Length);
return (Astruct)bf.Deserialize( ms ); //<---从byte数组再转回来
}
}
uing System.Runtime.Serialization.Formatters.Binary;
//...
[Serializable]
public struct Astruct
{
public int seq;
public char[] name;
public ulong len;
public char[] data;
public byte[] GetSerializedBytes()
{
BinaryFormatter bf = new BinaryFormatter(); //<---
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bf.Serialize(ms, this); //<---系列化成byte数组
ms.Seek(0, System.IO.SeekOrigin.Begin);
return ms.ToArray();
}
}
public static Astruct FromSerilizedBytes(byte[] data)
{
BinaryFormatter bf = new BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
return (Astruct)bf.Deserialize( ms ); //<---从byte数组再转回来
}
}
}
public static byte[] Encode(string codeString)
{
UnicodeEncoding encode = new UnicodeEncoding ( ) ;
return encode.GetBytes(codeString);
}
byte[] sendData =Encode(udprequest.requestInfo);