1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Drawing;  
  11. using System.Drawing.Imaging;  
  12. using System.Runtime.InteropServices;  
  13.  
  14.  
  15. /// <summary>  
  16.     /// 用memcmp比较字节数组  
  17.     /// </summary>  
  18.     /// <param name="b1">字节数组1</param>  
  19.     /// <param name="b2">字节数组2</param>  
  20.     /// <returns>如果两个数组相同,返回0;如果数组1小于数组2,返回小于0的值;如果数组1大于数组2,返回大于0的值。</returns>  
  21.     public static int MemoryCompare(byte[] b1, byte[] b2)  
  22.     {  
  23.         IntPtr retval = memcmp(b1, b2, new IntPtr(b1.Length));  
  24.         return retval.ToInt32();  
  25.     }  
  26.  
  27.     /// <summary>  
  28.     /// 比较字节数组  
  29.     /// </summary>  
  30.     /// <param name="b1">字节数组1</param>  
  31.     /// <param name="b2">字节数组2</param>  
  32.     /// <returns>如果两个数组相同,返回0;如果数组1小于数组2,返回小于0的值;如果数组1大于数组2,返回大于0的值。</returns>  
  33.     public static int MemoryCompare2(byte[] b1, byte[] b2)  
  34.     {  
  35.         int result = 0;  
  36.         if (b1.Length != b2.Length)  
  37.             result = b1.Length - b2.Length;  
  38.         else 
  39.         {  
  40.             for (int i = 0; i < b1.Length; i++)  
  41.             {  
  42.                 if (b1[i] != b2[i])  
  43.                 {  
  44.                     result = (int)(b1[i] - b2[i]);  
  45.                     break;  
  46.                 }  
  47.             }  
  48.         }  
  49.         return result;  
  50.     }  
  51.  
  52.     /// <summary>  
  53.     /// memcmp API  
  54.     /// </summary>  
  55.     /// <param name="b1">字节数组1</param>  
  56.     /// <param name="b2">字节数组2</param>  
  57.     /// <returns>如果两个数组相同,返回0;如果数组1小于数组2,返回小于0的值;如果数组1大于数组2,返回大于0的值。</returns>  
  58.     [DllImport("msvcrt.dll")]  
  59.     private static extern IntPtr memcmp(byte[] b1, byte[] b2, IntPtr count);  
  60. }  
  61.  
  62.  
  63.  
  64.    
  65.