zoukankan      html  css  js  c++  java
  • c#用memcmp比较字节数组 dodo

    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);
    }

  • 相关阅读:
    Could not continue scan with NOLOCK due to data movement
    Paxos共识算法详解
    从CAP到Paxos算法
    一文读懂拜占庭将军问题
    Paxos 与拜占庭将军问题
    CAP、BASEd、二阶段提交协议、三阶段提交协议、拜占庭将军问题、paxos、Raft、ZAB、NWR
    拜占庭将军
    三分钟教您看懂中本聪是如何解决拜占庭将军问题的
    SimpleDateFormat线程不安全原因及解决方案
    mysql having多个条件
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/1522546.html
Copyright © 2011-2022 走看看