C#里使用CopyMemory

Socket接收到的byte []要转换成自定义的struct / 自定义Struct转换成byte []都相当麻烦
用循环去转换太浪费时间了……于是想到用CopyMemory,Google一圈终于搞定
下面的代码是在Snippet Compiler里编译通过的

C#代码
  1. #region Imports   
  2. using System;   
  3. using System.IO;   
  4. using System.Net;   
  5. using System.Xml;   
  6. using System.Text;   
  7. using System.Data;   
  8. using System.Drawing;   
  9. using System.Threading;   
  10. using System.Reflection;   
  11. using System.Collections;   
  12. using System.Net.Sockets;   
  13. using System.Windows.Forms;   
  14. using System.ComponentModel;   
  15. using System.Drawing.Imaging;   
  16. using System.Security.Cryptography;   
  17. using System.Runtime.InteropServices;   
  18. using System.Text.RegularExpressions;  
  19. #endregion  
  20. #region Assembly Info   
  21. [assembly: AssemblyTitle("Test Application by eglic")]   
  22. [assembly: AssemblyDescription("Test Application by eglic")]   
  23. [assembly: AssemblyConfiguration("")]   
  24. [assembly: AssemblyCompany("eGlic.Com")]   
  25. [assembly: AssemblyProduct("Test Application by eglic")]   
  26. [assembly: AssemblyCopyright("Copyright (C) eGlic.Com 2007")]   
  27. [assembly: AssemblyTrademark("eGlic.Com")]   
  28. [assembly: AssemblyCulture("")]   
  29. [assembly: ComVisible(false)]   
  30. [assembly: AssemblyVersion("1.0.0.0")]   
  31. [assembly: AssemblyFileVersion("1.0.0.0")]  
  32. #endregion   
  33.   
  34. namespace eGlic   
  35. {  
  36.     #region Application Entrance   
  37.     public class Test{   
  38.         [STAThread]   
  39.         public static void Main() {   
  40.             byte [] buffer=new byte [20];   
  41.             DataGraphHeader header=new DataGraphHeader();   
  42.             string data="ABCDEFGH";   
  43.                
  44.             header.Signature=0xFF;   
  45.             header.Protocol.Type=1;   
  46.             header.Protocol.Version=1;   
  47.             header.Type=99;   
  48.             header.SerialID=1234567;   
  49.             header.Size=8;   
  50.                
  51.             Win32API.CopyMemoryEx(buffer,ref header);   
  52.             Win32API.CopyMemoryEx(buffer,12,System.Text.Encoding.ASCII.GetBytes(data),0,8);   
  53.                
  54.             string o="";   
  55.             for(int i=0;i<buffer.Length;i++){   
  56.                 if(buffer[i]<10) o+="0";   
  57.                 o+=String.Format("{0:X}",buffer[i]);   
  58.                 o+=" ";   
  59.             }   
  60.             MessageBox.Show(o,"转成Byte []之后的数据包",MessageBoxButtons.OK,MessageBoxIcon.Information);   
  61.                
  62.             DataGraphHeader h=new DataGraphHeader();   
  63.             byte [] buf;   
  64.             string d="";   
  65.                
  66.             Win32API.CopyMemoryEx(ref h,buffer);   
  67.             buf=new byte [h.Size];   
  68.             Win32API.CopyMemoryEx(buf,buffer,12,h.Size);   
  69.                
  70.             o="h.Signature=" +h.Signature.ToString()+"/r/n";   
  71.             o+="h.Protocol.Type=" +h.Protocol.Type.ToString()+"/r/n";   
  72.             o+="h.Protocol.Version=" +h.Protocol.Version.ToString()+"/r/n";   
  73.             o+="h.Type=" +h.Type.ToString()+"/r/n";   
  74.             o+="h.SerialID=" +h.SerialID.ToString()+"/r/n";   
  75.             o+="h.Size=" +h.Size.ToString()+"/r/n";   
  76.             o+="附加数据为:"+System.Text.Encoding.ASCII.GetString(buf);   
  77.             MessageBox.Show(o,"解析后数据包",MessageBoxButtons.OK,MessageBoxIcon.Information);   
  78.         }   
  79.            
  80.     }  
  81.     #endregion  
  82.       
  83.     #region Win32API   
  84.     public class Win32API {   
  85.         [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)]   
  86.         public extern static long CopyMemory(IntPtr dest, IntPtr source, int size);  
  87.         #region CopyMemoryEx   
  88.         /// <summary>   
  89.         /// CopyMemoryEx   
  90.         /// </summary>   
  91.         /// <param name="dest">目标缓存区</param>   
  92.         /// <param name="source">DataGraphPackage</param>   
  93.         /// <returns></returns>   
  94.         public unsafe static long CopyMemoryEx(byte[] dest, ref DataGraphHeader source) {   
  95.             return CopyMemoryEx(dest, 0,ref source);   
  96.         }   
  97.         /// <summary>   
  98.         /// CopyMemoryEx   
  99.         /// </summary>   
  100.         /// <param name="dest">目标缓存区</param>   
  101.         /// <param name="DestStart">目标缓存区中的开始位置</param>   
  102.         /// <param name="source">DataGraphPackage</param>   
  103.         /// <returns></returns>   
  104.         public unsafe static long CopyMemoryEx(byte[] dest,int DestStart, ref DataGraphHeader source) {   
  105.             IntPtr dp;   
  106.             IntPtr sp;   
  107.             fixed (byte* ds = &dest[DestStart]) {   
  108.                 fixed (DataGraphHeader* sr = &source) {   
  109.                     dp = (IntPtr)ds;   
  110.                     sp = (IntPtr)sr;   
  111.                     return CopyMemory(dp, sp, sizeof(DataGraphHeader));   
  112.                 }   
  113.             }   
  114.         }   
  115.         /// <summary>   
  116.         /// CopyMemoryEx   
  117.         /// </summary>   
  118.         /// <param name="dest">DataGraphPackage</param>   
  119.         /// <param name="source">源数据缓存</param>   
  120.         /// <returns></returns>   
  121.         public unsafe static long CopyMemoryEx(ref DataGraphHeader dest, byte[] source) {   
  122.             return CopyMemoryEx(ref dest, source, 0);   
  123.         }   
  124.         /// <summary>   
  125.         /// CopyMemoryEx   
  126.         /// </summary>   
  127.         /// <param name="dest">DataGraphPackage</param>   
  128.         /// <param name="source">源数据缓存</param>   
  129.         /// <returns></returns>   
  130.         public unsafe static long CopyMemoryEx(ref DataGraphHeader dest, byte[] source,int SourceStart) {   
  131.             IntPtr dp;   
  132.             IntPtr sp;   
  133.             fixed (DataGraphHeader* ds = &dest) {   
  134.                 fixed (byte* sr = &source[SourceStart]) {   
  135.                     dp = (IntPtr)ds;   
  136.                     sp = (IntPtr)sr;   
  137.                     return CopyMemory(dp, sp, sizeof(DataGraphHeader));   
  138.                 }   
  139.             }   
  140.         }   
  141.         /// <summary>   
  142.         /// CopyMemoryEx   
  143.         /// </summary>   
  144.         /// <param name="dest">目标缓存</param>   
  145.         /// <param name="source">源数据</param>   
  146.         /// <param name="size">要从源数据中复制的长度</param>   
  147.         /// <returns></returns>   
  148.         public unsafe static long CopyMemoryEx(byte[] dest, byte[] source, int size) {   
  149.             return CopyMemoryEx(dest, 0, source, 0, size);   
  150.         }   
  151.         /// <summary>   
  152.         /// CopyMemoryEx   
  153.         /// </summary>   
  154.         /// <param name="dest">目标缓存</param>   
  155.         /// <param name="source">源数据</param>   
  156.         /// <param name="SourceStart">源数据缓存中开始位置</param>   
  157.         /// <param name="size">要从源数据中复制的长度</param>   
  158.         /// <returns></returns>   
  159.         public unsafe static long CopyMemoryEx(byte[] dest, byte[] source, int SourceStart,int size) {   
  160.             return CopyMemoryEx(dest, 0, source, SourceStart, size);   
  161.         }   
  162.         /// <summary>   
  163.         /// CopyMemoryEx   
  164.         /// </summary>   
  165.         /// <param name="dest">目标缓存</param>   
  166.         /// <param name="DestStart">目标缓存中开始复制的位置</param>   
  167.         /// <param name="source">源数据</param>   
  168.         /// <param name="SourceStart">源数据缓存中开始位置</param>   
  169.         /// <param name="size">要从源数据中复制的长度</param>   
  170.         /// <returns></returns>   
  171.         public unsafe static long CopyMemoryEx(byte[] dest,int DestStart, byte[] source, int SourceStart, int size) {   
  172.             IntPtr dp;   
  173.             IntPtr sp;   
  174.             fixed (byte* ds = &dest[DestStart]) {   
  175.                 fixed (byte* sr = &source[SourceStart]) {   
  176.                     dp = (IntPtr)ds;   
  177.                     sp = (IntPtr)sr;   
  178.                     return CopyMemory(dp, sp, size);   
  179.                 }   
  180.             }   
  181.         }  
  182.         #endregion   
  183.     }  
  184.     #endregion   
  185.        
  186.     [StructLayout(LayoutKind.Sequential)]   
  187.     public struct ProtocolInfo {   
  188.         public byte Type;   
  189.         public byte Version;   
  190.     }   
  191.        
  192.     [StructLayout(LayoutKind.Sequential)]   
  193.     public struct DataGraphHeader {   
  194.         public byte Signature;              //包头:    1字节   
  195.         public ProtocolInfo Protocol;       //协议:    2字节   
  196.         public byte Type;                   //包类型:  1字节   
  197.         public uint SerialID;               //包序号    4字节   
  198.         public int Size;                    //包尺寸    4字节   
  199.     }   
  200. }  
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值