using System;
using System.Collections.Generic;
using System.Text;
namespace WIDESEA_Common
{
public static class Extension
{
///
/// 从Byte数组中提取所有的位数组
/// Extracts a bit array from a byte array, length represents the number of digits
///
/// 原先的字节数组
/// 转换后的bool数组
public static bool[] ToBoolArray(this byte[] InBytes)
{
return ByteToBoolArray(InBytes, InBytes.Length * 8);
}
///
/// 从Byte数组中提取位数组,length代表位数
/// Extracts a bit array from a byte array, length represents the number of digits
///
/// 原先的字节数组
/// 想要转换的长度,如果超出自动会缩小到数组最大长度
/// 转换后的bool数组
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;
}
///
/// 获取byte数据类型的第offset位,是否为True
/// Gets the index bit of the byte data type, whether it is True
///
/// byte数值
/// 索引位置
/// 结果
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;
}
///
/// 获取到数组里面的中间指定长度的数组
/// Get an array of the specified length in the array
///
/// 数组
/// 起始索引
/// 数据的长度
/// 新的数组值
public static T[] ArraySelectMiddle(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(this T[] value, int index, int length)
{
return ArraySelectMiddle(value, index, length);
}
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
}
}