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