約編組這些數據是什麼?如果你有,例如,含有用於「移動式」的結構體,你可以這樣做:
發件人:
SOME_STRUCT data = new SOME_STRUCT();
int structSize = Marshal.SizeOf(data);
// you fill your struct here
var msgBytes = new Byte[1024];
IntPtr pointer = Marshal.AllocHGlobal(structSize);
Marshal.StructureToPtr(data, pointer, true);
Marshal.Copy(pointer, msgBytes, 0, size);
Marshal.FreeHGlobal(pointer);
接收機:
SOME_STRUCT receivedData = new SOME_STRUCT();
int structSize = Marshal.SizeOf(data);
// You receive your data here
var receivedBytes = msgBytes;
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
IntPtr pointer = handle.AddrOfPinnedObject();
Marshal.Copy(receivedBytes, 0, pointer, (int)structSize);
作爲一個小紙條,'寫(字符串)「與其他非.NET系統具有」非高「互操作性(因爲它以特定的方式格式化字符串,在字符串前添加7位編碼長度),PLUS默認編碼」BinaryWriter「是UTF8(這不是問題,只是一個信息)。其他的'Write(something)'都非常簡單,所以它們具有非常高的互操作性(ok ...'Write(decimal)'具有非常低的互操作性,因爲'decimal'只是.NET) – xanatos