qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Text;
 
namespace WIDESEA_Common
{
    public static class Extension
    {
        /// <summary>
        /// 从Byte数组中提取所有的位数组<br />
        /// Extracts a bit array from a byte array, length represents the number of digits
        /// </summary>
        /// <param name="InBytes">原先的字节数组</param>
        /// <returns>转换后的bool数组</returns>
        public static bool[] ToBoolArray(this byte[] InBytes)
        {
            return ByteToBoolArray(InBytes, InBytes.Length * 8);
        }
 
        /// <summary>
        /// 从Byte数组中提取位数组,length代表位数<br />
        /// Extracts a bit array from a byte array, length represents the number of digits
        /// </summary>
        /// <param name="InBytes">原先的字节数组</param>
        /// <param name="length">想要转换的长度,如果超出自动会缩小到数组最大长度</param>
        /// <returns>转换后的bool数组</returns>
        public static bool[] ByteToBoolArray(byte[] InBytes, int length)
        {
            bool flag = InBytes == null;
            bool[] result;
            if (flag)
            {
                result = null;
            }
            else
            {
                bool flag2 = length > InBytes.Length * 8;
                if (flag2)
                {
                    length = InBytes.Length * 8;
                }
                bool[] array = new bool[length];
                for (int i = 0; i < length; i++)
                {
                    array[i] = BoolOnByteIndex(InBytes[i / 8], i % 8);
                }
                result = array;
            }
            return result;
        }
 
        /// <summary>
        /// 获取byte数据类型的第offset位,是否为True<br />
        /// Gets the index bit of the byte data type, whether it is True
        /// </summary>
        /// <param name="value">byte数值</param>
        /// <param name="offset">索引位置</param>
        /// <returns>结果</returns>
        private static byte GetDataByBitIndex(int offset)
        {
            byte result;
            switch (offset)
            {
                case 0:
                    result = 1;
                    break;
                case 1:
                    result = 2;
                    break;
                case 2:
                    result = 4;
                    break;
                case 3:
                    result = 8;
                    break;
                case 4:
                    result = 16;
                    break;
                case 5:
                    result = 32;
                    break;
                case 6:
                    result = 64;
                    break;
                case 7:
                    result = 128;
                    break;
                default:
                    result = 0;
                    break;
            }
            return result;
        }
 
        public static bool BoolOnByteIndex(byte value, int offset)
        {
            byte dataByBitIndex = GetDataByBitIndex(offset);
            return (value & dataByBitIndex) == dataByBitIndex;
        }
 
        /// <summary>
        /// 获取到数组里面的中间指定长度的数组<br />
        /// Get an array of the specified length in the array
        /// </summary>
        /// <param name="value">数组</param>
        /// <param name="index">起始索引</param>
        /// <param name="length">数据的长度</param>
        /// <returns>新的数组值</returns>
        public static T[] ArraySelectMiddle<T>(T[] value, int index, int length)
        {
            bool flag = value == null;
            T[] result;
            if (flag)
            {
                result = null;
            }
            else
            {
                T[] array = new T[Math.Min(value.Length, length)];
                Array.Copy(value, index, array, 0, array.Length);
                result = array;
            }
            return result;
        }
 
        public static T[] SelectMiddle<T>(this T[] value, int index, int length)
        {
            return ArraySelectMiddle<T>(value, index, length);
        }
 
        public static string ObjToString(this object thisValue)
        {
            if (thisValue != null) return thisValue.ToString().Trim();
            return "";
        }
    }
}