byte[]、list<byte>数组类型的几个自定义扩展方法

发布时间 2023-11-07 15:01:39作者: 海宏软件

byte[]、list<byte>数组类型的几个自定义扩展方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace iPublic.类型扩展方法
{
    /// <summary>
    /// 类型的扩展方法,用起来方便的
    /// 修改记录:
    ///   20230415,海宏软件,zch,List的
    /// </summary>
    public static class ExtendMethods
    {


        /// <summary>
        /// 十六进制字符串转成byte数组,用空格、减号分割
        /// </summary>
        /// <param name="InString"></param>
        /// <returns></returns>
        public static byte[] HexStringToByte(this string InString)
        {
            string[] ByteStrings;
            ByteStrings = InString.Split(' ', '-');
            byte[] ByteOut;
            ByteOut = new byte[ByteStrings.Length];
            for (int i = 0; i <= ByteStrings.Length - 1; i++)
            {
                ByteOut[i] = Byte.Parse(ByteStrings[i], System.Globalization.NumberStyles.HexNumber);
            }
            return ByteOut;
        }


        #region //扩展List<int>的ToString方法
        /// <summary>
        /// 扩展List<int>的ToString方法,注意,如果参数和基类一样,会调用基类的,这里就不执行了
        /// </summary>
        /// <param name="list"></param>
        /// <param name="连接符"></param>
        /// <returns></returns>
        public static string ToString(this List<byte> list, string 连接符)
        {
            if (list == null || list.Count < 1) return "";
            string s = "", sChar = 连接符, s2 = "";
            if (string.IsNullOrEmpty(sChar)) sChar = " ";
            //传统转换法,速度太慢,5M大概需要2分钟多
            //foreach (byte itm in list) s += sChar + itm.ToString("X2");
            //系统自带转换法,5M大概需要2秒
            s = BitConverter.ToString(list.ToArray());
            if (!string.IsNullOrEmpty(sChar)) s = s.Replace("-", sChar);
            
            //完成
            return s;
        }
        #endregion


        #region //扩展byte[]的ToString
        /// <summary>
        /// 扩展byte[]的ToString方法,注意,如果参数和基类一样,会调用基类的,这里就不执行了
        /// </summary>
        /// <param name="list"></param>
        /// <param name="连接符"></param>
        /// <returns></returns>
        public static string ToString(this byte[] list, string 连接符)
        {
            if (list == null || list.Length < 1) return "";
            string s = "", s2 = "", sChar = 连接符;
            //传统转换法速度太慢,5M大概需要2分钟多
            //foreach (byte itm in list) s += (s == "" ? "" : 连接符) + itm.ToString("X2");
            //系统自带转换法,5M大概需要2秒
            s = BitConverter.ToString(list);
            if (!string.IsNullOrEmpty(sChar)) s = s.Replace("-", sChar);
            //完成
            return s;
        }
        #endregion


        #region //IndexOfByte数组
        /// <summary>  
        /// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引。
        /// 参考:https://www.iteye.com/blog/testcs-dn-2099443
        /// </summary>  
        /// <param name="srcBytes">被执行查找的 System.Byte[]。</param>  
        /// <param name="searchBytes">要查找的 System.Byte[]。</param>  
        /// <returns>如果找到该字节数组,则为 searchBytes 的索引位置;如果未找到该字节数组,则为 -1。如果 searchBytes 为 null 或者长度为0,则返回值为 -1。</returns>  
        public static int IndexOf(this byte[] srcBytes, byte[] searchBytes)
        {
            return IndexOf(srcBytes, searchBytes, 0);
        }
        public static int IndexOf(this byte[] srcBytes, byte[] searchBytes, int 开始位置)
        {
            if (srcBytes == null) { return -1; }
            if (searchBytes == null) { return -1; }
            if (srcBytes.Length == 0) { return -1; }
            if (searchBytes.Length == 0) { return -1; }
            if (srcBytes.Length < searchBytes.Length) { return -1; }
            int nSrc = srcBytes.Length, nSearch = searchBytes.Length;
            for (int i = 开始位置; i < nSrc /*- searchBytes.Length*/; i++)
            {
                if (srcBytes[i] != searchBytes[0]) continue;
                if (nSearch == 1) { return i; }
                if (i + nSearch > nSrc) return -1;  //查找

                bool flag = true;
                for (int j = 1; j < nSearch; j++)
                {
                    if (srcBytes[i + j] != searchBytes[j])
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    return i;
                }
            }
            return -1;
        }
        #endregion


        #region //byte[].split拆分成多个数组
        public static List<byte[]> split(this byte[] src, List<byte[]> separator)
        {
            List<byte[]> result = new List<byte[]>();
            byte[] buf = null, bufAll = src;
            //Dictionary<int, int> indexs = new Dictionary<int, int>();
            int nIdx = 0, nLastIdx = 0, nLen = src.Length;
            while (nLastIdx < src.Length)
            {
                byte[] finder = null;
                foreach (var itm in separator)
                {
                    nIdx = src.IndexOf(itm, nLastIdx);  //查找
                    if (nIdx >= 0) { finder = itm; break; } //找到了,记下来
                }
                if (finder != null)
                {
                    if (nIdx <= nLastIdx) result.Add(null);  //第一位就是
                    else
                    {   //取出有效数据,放入结果中
                        buf = src.Skip(nLastIdx).Take(nIdx - nLastIdx).ToArray();
                        result.Add(buf);
                    }
                    nLastIdx = nIdx + finder.Length;    //上次的位置
                }
                else
                {   //全部
                    if (src.Length > nLastIdx)
                    {
                        buf = src.Skip(nLastIdx).Take(src.Length - nLastIdx).ToArray();
                        result.Add(buf);
                    }
                    nLastIdx = src.Length;
                }
            }
            //
            return result;
        }
        #endregion


    }



}

  

想不到BitConvert.ToString的速度会差这么多。用法:

using namespace iPublic.类型扩展方法;

byte[] buf=new byte[];

.........

string s=buf.ToString("");