using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CommonExtend
{
///
/// 字节扩展方法
///
public static class ByteExtend
{
///
/// 字节数组转16进制字符串:空格分隔
///
///
///
public static string Byte2HexString(this byte[] byteDatas)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < byteDatas.Length; i++)
{
builder.Append(string.Format("{0:X2}", byteDatas[i]));
}
return builder.ToString().Trim();
}
///
/// Hex字符串转Byte数组
///
/// 字符串
///
public static byte[] HexString2Byte(this string hex)
{
var inputByteArray = new byte[hex.Length / 2];
for (var x = 0; x < inputByteArray.Length; x++)
{
var i = Convert.ToInt32(hex.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
return inputByteArray;
}
public static int Byte2HexSum(this byte[] byteDatas)
{
var Sum = 0;
foreach (var byteData in byteDatas)
{
Sum += new byte[] { byteData }.Byte2HexString().Hex2Int();
}
return Sum;
}
///
/// 字节byte 转int
///
///
///
public static int Byte2Int(this byte[] data)
{
if (data.Length <= 1)
{
data = data.BytePadLeft(2);
}
if (BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToUInt16(data, 0);
}
///
/// 字节byte 转int
///
///
///
public static byte[] Int2Byte(this int data)
{
var Temp = BitConverter.GetBytes(data);
if (BitConverter.IsLittleEndian)
Array.Reverse(Temp);
return Temp;
}
///
/// 字节byte 转string
///
/// 字节数组
///
public static string Byte2String(this byte[] data)
{
return System.Text.Encoding.UTF8.GetString(data);
}
///
/// 字符串转byte数组
///
/// 字符串
///
public static byte[] String2Byte(this string data)
{
return System.Text.Encoding.UTF8.GetBytes(data);
}
///
/// 字节byte 转bit数组
///
/// 字节数组
///
public static BitArray Byte2BitArray(this byte[] data)
{
return new BitArray(data);
}
///
/// 字节byte 转bit数组
///
/// 字节数组
///
public static int Byte2Bit(this byte[] data, short index)
{
var _Bits = new BitArray(data);
return _Bits[index] ? 1 : 0;
}
public static byte[] BitArray2Byte(this BitArray bitArray)
{
bitArray.Length = 8;
byte[] Temp = new byte[1];
bitArray.CopyTo(Temp, 0);
return Temp;
}
public static byte[] BitArray2Byte(this int[] bitArray)
{
BitArray bitArray1 = new BitArray(new bool[] {
Convert.ToBoolean(bitArray[0]),
Convert.ToBoolean(bitArray[1]),
Convert.ToBoolean(bitArray[2]),
Convert.ToBoolean(bitArray[3]),
Convert.ToBoolean(bitArray[4]),
Convert.ToBoolean(bitArray[5]),
Convert.ToBoolean(bitArray[6]),
Convert.ToBoolean(bitArray[7])
});
return BitArray2Byte(bitArray1);
}
public static int[] Int2Bit(this int data, int length)
{
string Temp = Convert.ToString(data, 2).PadLeft(length, '0');
int[] Temp1 = new int[length];
for (var i = 0; i < length; i++)
{
Temp1[i] = Convert.ToInt32(Temp.Substring(i, 1));
}
return Temp1;
}
///
/// 根据长度补全字节数组
///
/// 字节数组
/// 长度
///
public static byte[] BytePadLeft(this IEnumerable data, int length)
{
var _List = data.ToList();
if (_List.Count < length)
{
for (var i = 0; i < length - data.Count(); i++)
{
_List.Insert(0, new byte());
}
}
else if (_List.Count > length)
{
for (var i = 0; i < data.Count() - length; i++)
{
_List.RemoveAt(0);
}
}
return _List.ToArray();
}
}
}