You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.7 KiB
C#

11 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CommonExtend
{
/// <summary>
/// 字节扩展方法
/// </summary>
public static class ByteExtend
{
/// <summary>
/// 字节数组转16进制字符串空格分隔
/// </summary>
/// <param name="byteDatas"></param>
/// <returns></returns>
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();
}
/// <summary>
/// Hex字符串转Byte数组
/// </summary>
/// <param name="hex">字符串</param>
/// <returns></returns>
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;
}
/// <summary>
/// 字节byte 转int
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 字节byte 转int
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Int2Byte(this int data)
{
var Temp = BitConverter.GetBytes(data);
if (BitConverter.IsLittleEndian)
Array.Reverse(Temp);
return Temp;
}
/// <summary>
/// 字节byte 转string
/// </summary>
/// <param name="data">字节数组</param>
/// <returns></returns>
public static string Byte2String(this byte[] data)
{
return System.Text.Encoding.UTF8.GetString(data);
}
/// <summary>
/// 字符串转byte数组
/// </summary>
/// <param name="data">字符串</param>
/// <returns></returns>
public static byte[] String2Byte(this string data)
{
return System.Text.Encoding.UTF8.GetBytes(data);
}
/// <summary>
/// 字节byte 转bit数组
/// </summary>
/// <param name="data">字节数组</param>
/// <returns></returns>
public static BitArray Byte2BitArray(this byte[] data)
{
return new BitArray(data);
}
/// <summary>
/// 字节byte 转bit数组
/// </summary>
/// <param name="data">字节数组</param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据长度补全字节数组
/// </summary>
/// <param name="data">字节数组</param>
/// <param name="length">长度</param>
/// <returns></returns>
public static byte[] BytePadLeft(this IEnumerable<byte> 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();
}
}
}