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

  • 相关阅读:
    图片api
    基于NoneBot的天气查询插件
    在Linux云服务上运行酷Q机器人并DIY自己的功能
    破解zip密码的几种方法
    攻防世界wp--crypto 幂数加密
    攻防世界wp--crypto Caesar
    攻防世界wp--web command_execution
    攻防世界wp--web weak_auth
    python中yield的用法以及和yield from的区别
    Linux进阶之正则,shell三剑客(grep,awk,sed),cut,sort,uniq
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/1522546.html
Copyright © 2020-2023  润新知