Socket接收到的byte []要轉換成自訂的struct / 自訂Struct轉換成byte []都相當麻煩
用迴圈去轉換太浪費時間了……於是想到用CopyMemory,Google一圈終於搞定
下面的代碼是在Snippet Compiler裡編譯通過的 C#代碼 #region Imports using System; using System.IO; using System.Net; using System.Xml; using System.Text; using System.Data; using System.Drawing; using System.Threading; using System.Reflection; using System.Collections; using System.Net.Sockets; using System.Windows.Forms; using System.ComponentModel; using System.Drawing.Imaging; using System.Security.Cryptography; using System.Runtime.InteropServices; using System.Text.RegularExpressions; #endregion #region Assembly Info [assembly: AssemblyTitle("Test Application by eglic")] [assembly: AssemblyDescription("Test Application by eglic")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("eGlic.Com")] [assembly: AssemblyProduct("Test Application by eglic")] [assembly: AssemblyCopyright("Copyright (C) eGlic.Com 2007")] [assembly: AssemblyTrademark("eGlic.Com")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] #endregion namespace eGlic { #region Application Entrance public class Test{ [STAThread] public static void Main() { byte [] buffer=new byte [20]; DataGraphHeader header=new DataGraphHeader(); string data="ABCDEFGH"; header.Signature=0xFF; header.Protocol.Type=1; header.Protocol.Version=1; header.Type=99; header.SerialID=1234567; header.Size=8; Win32API.CopyMemoryEx(buffer,ref header); Win32API.CopyMemoryEx(buffer,12,System.Text.Encoding.ASCII.GetBytes(data),0,8); string o=""; for(int i=0;i<buffer.Length;i++){ if(buffer[i]<10) o+="0"; o+=String.Format("{0:X}",buffer[i]); o+=" "; } MessageBox.Show(o,"轉成Byte []之後的資料包",MessageBoxButtons.OK,MessageBoxIcon.Information); DataGraphHeader h=new DataGraphHeader(); byte [] buf; string d=""; Win32API.CopyMemoryEx(ref h,buffer); buf=new byte [h.Size]; Win32API.CopyMemoryEx(buf,buffer,12,h.Size); o="h.Signature=" +h.Signature.ToString()+"/r/n"; o+="h.Protocol.Type=" +h.Protocol.Type.ToString()+"/r/n"; o+="h.Protocol.Version=" +h.Protocol.Version.ToString()+"/r/n"; o+="h.Type=" +h.Type.ToString()+"/r/n"; o+="h.SerialID=" +h.SerialID.ToString()+"/r/n"; o+="h.Size=" +h.Size.ToString()+"/r/n"; o+="附加資料為:"+System.Text.Encoding.ASCII.GetString(buf); MessageBox.Show(o,"解析後資料包",MessageBoxButtons.OK,MessageBoxIcon.Information); } } #endregion #region Win32API public class Win32API { [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)] public extern static long CopyMemory(IntPtr dest, IntPtr source, int size); #region CopyMemoryEx /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">目標緩衝區</param> /// <param name="source">DataGraphPackage</param> /// <returns></returns> public unsafe static long CopyMemoryEx(byte[] dest, ref DataGraphHeader source) { return CopyMemoryEx(dest, 0,ref source); } /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">目標緩衝區</param> /// <param name="DestStart">目標緩衝區中的開始位置</param> /// <param name="source">DataGraphPackage</param> /// <returns></returns> public unsafe static long CopyMemoryEx(byte[] dest,int DestStart, ref DataGraphHeader source) { IntPtr dp; IntPtr sp; fixed (byte* ds = &dest[DestStart]) { fixed (DataGraphHeader* sr = &source) { dp = (IntPtr)ds; sp = (IntPtr)sr; return CopyMemory(dp, sp, sizeof(DataGraphHeader)); } } } /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">DataGraphPackage</param> /// <param name="source">來源資料緩衝</param> /// <returns></returns> public unsafe static long CopyMemoryEx(ref DataGraphHeader dest, byte[] source) { return CopyMemoryEx(ref dest, source, 0); } /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">DataGraphPackage</param> /// <param name="source">來源資料緩衝</param> /// <returns></returns> public unsafe static long CopyMemoryEx(ref DataGraphHeader dest, byte[] source,int SourceStart) { IntPtr dp; IntPtr sp; fixed (DataGraphHeader* ds = &dest) { fixed (byte* sr = &source[SourceStart]) { dp = (IntPtr)ds; sp = (IntPtr)sr; return CopyMemory(dp, sp, sizeof(DataGraphHeader)); } } } /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">目標緩衝</param> /// <param name="source">來源資料</param> /// <param name="size">要從來源資料中複製的長度</param> /// <returns></returns> public unsafe static long CopyMemoryEx(byte[] dest, byte[] source, int size) { return CopyMemoryEx(dest, 0, source, 0, size); } /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">目標緩衝</param> /// <param name="source">來源資料</param> /// <param name="SourceStart">來源資料緩衝中開始位置</param> /// <param name="size">要從來源資料中複製的長度</param> /// <returns></returns> public unsafe static long CopyMemoryEx(byte[] dest, byte[] source, int SourceStart,int size) { return CopyMemoryEx(dest, 0, source, SourceStart, size); } /// <summary> /// CopyMemoryEx /// </summary> /// <param name="dest">目標緩衝</param> /// <param name="DestStart">目標緩衝中開始複製的位置</param> /// <param name="source">來源資料</param> /// <param name="SourceStart">來源資料緩衝中開始位置</param> /// <param name="size">要從來源資料中複製的長度</param> /// <returns></returns> public unsafe static long CopyMemoryEx(byte[] dest,int DestStart, byte[] source, int SourceStart, int size) { IntPtr dp;