- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- /// <summary>
- /// 用memcmp比较字节数组
- /// </summary>
- /// <param name="b1">字节数组1</param>
- /// <param name="b2">字节数组2</param>
- /// <returns>如果两个数组相同,返回0;如果数组1小于数组2,返回小于0的值;如果数组1大于数组2,返回大于0的值。</returns>
- public static int MemoryCompare(byte[] b1, byte[] b2)
- {
- IntPtr retval = memcmp(b1, b2, new IntPtr(b1.Length));
- return retval.ToInt32();
- }
- /// <summary>
- /// 比较字节数组
- /// </summary>
- /// <param name="b1">字节数组1</param>
- /// <param name="b2">字节数组2</param>
- /// <returns>如果两个数组相同,返回0;如果数组1小于数组2,返回小于0的值;如果数组1大于数组2,返回大于0的值。</returns>
- public static int MemoryCompare2(byte[] b1, byte[] b2)
- {
- int result = 0;
- if (b1.Length != b2.Length)
- result = b1.Length - b2.Length;
- else
- {
- for (int i = 0; i < b1.Length; i++)
- {
- if (b1[i] != b2[i])
- {
- result = (int)(b1[i] - b2[i]);
- break;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// memcmp API
- /// </summary>
- /// <param name="b1">字节数组1</param>
- /// <param name="b2">字节数组2</param>
- /// <returns>如果两个数组相同,返回0;如果数组1小于数组2,返回小于0的值;如果数组1大于数组2,返回大于0的值。</returns>
- [DllImport("msvcrt.dll")]
- private static extern IntPtr memcmp(byte[] b1, byte[] b2, IntPtr count);
- }
转载于:https://blog.51cto.com/cnming/610514