使用序列化 [Serializable] public struct test { public int x; }class Class1 { public static void abc() { byte[] b; System.Runtime.Serialization.IFormatter f = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); test struct1, struct2; struct1.x = 100; //将struct1序列化到b中 System.IO.MemoryStream ms1 = new MemoryStream(); f.Serialize(ms1, struct1); b = ms1.ToArray(); ms1.Close(); //从b中还原给struct2 System.IO.MemoryStream ms2 = new MemoryStream(b); struct2 = (test)f.Deserialize(ms2); ms2.Close(); } }
to bdhh(Silent), 也看到过一篇文章,使用"序列化",但转换后的结果是有问题的,除了数据成员以外,还有一些其他信息,所以一个简单的结构也至少100多字节!!这样通过Socket在不同语言的程序中发送信息是不行的???我倒通过重载操作符做到了,不知道还有其他简便的方法没有??????
public struct Test { public int i; public double d; public char c; } unsafe public byte[] StructToBytes(Test test) { int size = System.Runtime.InteropServices.Marshal.SizeOf(test); byte[] bytes = new byte[size]; fixed (byte* p = &bytes[0]) { System.Runtime.InteropServices.Marshal.StructureToPtr(test, (IntPtr)p, true); } return bytes; } unsafe public Test BytesToStruct(byte[] bytes) { fixed (byte* p = &bytes[0]) { return (Test)System.Runtime.InteropServices.Marshal.PtrToStructure((IntPtr)p, typeof(Test)); } } private void Form1_Load(object sender, System.EventArgs e) { Test test; test.i = 10; test.d = 15.3; test.c = (char)65; byte[] b = StructToBytes(test); Test t = BytesToStruct(b); }
pupo(泡泡)这种方法在跨应用程序域的情况下处理字符串有问题,因为string是引用类型
internal struct DLPSoftWinCmdStruct { public int cmdID; public int left; public int top; public int right; public int bottom; public int windowIndex; public int windowSignalType; public int inputIndex; public int videoWindowIndex; public int rgbWindowIndex; public int netWindowIndex; /// <summary> /// 操作符重载,将结构强制转换成字节数组 /// </summary> /// <param name="t"></param> /// <returns></returns> public static explicit operator byte[](DLPSoftWinCmdStruct cmdClass) { MemoryStream fs = new MemoryStream(); fs.Write(BitConverter.GetBytes(cmdClass.cmdID), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.left), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.top), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.right), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.bottom), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.windowIndex), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.windowSignalType), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.inputIndex), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.videoWindowIndex), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.rgbWindowIndex), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.netWindowIndex), 0, 4); return fs.ToArray(); }
[Serializable]
public struct test
{
public int x;
}class Class1
{
public static void abc()
{
byte[] b;
System.Runtime.Serialization.IFormatter f = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
test struct1, struct2; struct1.x = 100;
//将struct1序列化到b中
System.IO.MemoryStream ms1 = new MemoryStream();
f.Serialize(ms1, struct1);
b = ms1.ToArray();
ms1.Close();
//从b中还原给struct2
System.IO.MemoryStream ms2 = new MemoryStream(b);
struct2 = (test)f.Deserialize(ms2);
ms2.Close();
}
}
也看到过一篇文章,使用"序列化",但转换后的结果是有问题的,除了数据成员以外,还有一些其他信息,所以一个简单的结构也至少100多字节!!这样通过Socket在不同语言的程序中发送信息是不行的???我倒通过重载操作符做到了,不知道还有其他简便的方法没有??????
{
public int i;
public double d;
public char c;
} unsafe public byte[] StructToBytes(Test test)
{
int size = System.Runtime.InteropServices.Marshal.SizeOf(test);
byte[] bytes = new byte[size];
fixed (byte* p = &bytes[0])
{
System.Runtime.InteropServices.Marshal.StructureToPtr(test, (IntPtr)p, true);
}
return bytes;
} unsafe public Test BytesToStruct(byte[] bytes)
{
fixed (byte* p = &bytes[0])
{
return (Test)System.Runtime.InteropServices.Marshal.PtrToStructure((IntPtr)p, typeof(Test));
}
} private void Form1_Load(object sender, System.EventArgs e)
{
Test test;
test.i = 10;
test.d = 15.3;
test.c = (char)65; byte[] b = StructToBytes(test);
Test t = BytesToStruct(b);
}
{
public int cmdID;
public int left;
public int top;
public int right;
public int bottom;
public int windowIndex;
public int windowSignalType;
public int inputIndex;
public int videoWindowIndex;
public int rgbWindowIndex;
public int netWindowIndex; /// <summary>
/// 操作符重载,将结构强制转换成字节数组
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static explicit operator byte[](DLPSoftWinCmdStruct cmdClass)
{
MemoryStream fs = new MemoryStream();
fs.Write(BitConverter.GetBytes(cmdClass.cmdID), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.left), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.top), 0, 4); fs.Write(BitConverter.GetBytes(cmdClass.right), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.bottom), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.windowIndex), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.windowSignalType), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.inputIndex), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.videoWindowIndex), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.rgbWindowIndex), 0, 4);
fs.Write(BitConverter.GetBytes(cmdClass.netWindowIndex), 0, 4); return fs.ToArray();
}
/// <summary>
/// 操作符重载,将字节数组强制转换为结构
/// </summary>
/// <param name="ba"></param>
/// <returns></returns>
public static explicit operator DLPSoftWinCmdStruct(byte[] byteBuffer)
{
if (byteBuffer.Length < sizeof(int) * 11)
{
throw new Exception("字节数组太短,无法转换成DLPSoftWinCmd类型");
}
MemoryStream fs = new MemoryStream(byteBuffer);
DLPSoftWinCmdStruct ret = new DLPSoftWinCmdStruct(); ret.cmdID = BitConverter.ToInt32(byteBuffer, 0);
ret.left = BitConverter.ToInt32(byteBuffer, sizeof(int));
ret.top = BitConverter.ToInt32(byteBuffer, sizeof(int) * 2); ret.right = BitConverter.ToInt32(byteBuffer, sizeof(int) * 3); ret.bottom = BitConverter.ToInt32(byteBuffer, sizeof(int) * 4);
ret.windowIndex = BitConverter.ToInt32(byteBuffer, sizeof(int) * 5);
ret.windowSignalType = BitConverter.ToInt32(byteBuffer, sizeof(int) * 6);
ret.inputIndex = BitConverter.ToInt32(byteBuffer, sizeof(int) * 7);
ret.videoWindowIndex = BitConverter.ToInt32(byteBuffer, sizeof(int) * 8);
ret.rgbWindowIndex = BitConverter.ToInt32(byteBuffer, sizeof(int) * 9);
ret.netWindowIndex = BitConverter.ToInt32(byteBuffer, sizeof(int) * 10); return ret;
}
}使用:
DLPSoftWinCmdStruct cmdData = new DLPSoftWinCmdStruct();
byte[] buff = null;
....
buff = (byte[])cmdData;
....
DLPSoftWinCmdStruct cmd2=null;
cmd2 = (DLPSoftWinCmdStruct)buff;
对于C++的字符串数组。使用GetString()或GetBytes方法。