|
|
|
@ -0,0 +1,198 @@
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace HybirdFrameworkCore.Utils;
|
|
|
|
|
|
|
|
|
|
public static class BitUtls
|
|
|
|
|
{
|
|
|
|
|
public static string Bytes2BinaryString(byte[] bytes)
|
|
|
|
|
{
|
|
|
|
|
return string.Join(" ", bytes.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 字节数组转16进制字符串:空格分隔
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="byteDatas"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string BytesToHexStr(byte[] byteDatas)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < byteDatas.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
builder.Append($"{byteDatas[i]:X2} ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.ToString().Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] HexStrToBytes(string hexStr)
|
|
|
|
|
{
|
|
|
|
|
//以 ' ' 分割字符串,并去掉空字符
|
|
|
|
|
string[] chars = hexStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
byte[] returnBytes = new byte[chars.Length];
|
|
|
|
|
//逐个字符变为16进制字节数据
|
|
|
|
|
for (int i = 0; i < chars.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
returnBytes[i] = Convert.ToByte(chars[i], 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnBytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte Byte2Bit(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
if (length > 8)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("length can not be greater then 8!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
byte result = 0;
|
|
|
|
|
int index = startBit / 8;
|
|
|
|
|
byte b = bytes[index];
|
|
|
|
|
|
|
|
|
|
int suffix = startBit % 8;
|
|
|
|
|
if (suffix == 0)
|
|
|
|
|
{
|
|
|
|
|
byte mask = (byte)(255 >> (8 - length));
|
|
|
|
|
result = (byte)(b & mask);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ((8 - suffix) >= length)
|
|
|
|
|
{
|
|
|
|
|
byte mask = (byte)(255 >> (8 - length));
|
|
|
|
|
result = (byte)(b & mask);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte pre = (byte)(b >> suffix);
|
|
|
|
|
|
|
|
|
|
int remain = length - (8 - suffix);
|
|
|
|
|
byte nextByte = bytes[index + 1];
|
|
|
|
|
|
|
|
|
|
nextByte = (byte)(nextByte << (8 - remain));
|
|
|
|
|
nextByte = (byte)(nextByte >> (8 - remain));
|
|
|
|
|
nextByte = (byte)(nextByte << (8 - suffix));
|
|
|
|
|
|
|
|
|
|
result = (byte)(pre | nextByte);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;*/
|
|
|
|
|
byte[] sub = Sub(bytes, startBit, length);
|
|
|
|
|
return sub[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string BytesToHexStr(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
byte[] sub = Sub(bytes, startBit, length);
|
|
|
|
|
|
|
|
|
|
return BytesToHexStr(sub);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 00 00 01000000
|
|
|
|
|
/// 00000000 01010000
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes"></param>
|
|
|
|
|
/// <param name="startBit"></param>
|
|
|
|
|
/// <param name="length"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static byte[] Sub(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
var skip = startBit % 8;
|
|
|
|
|
var index = startBit / 8;
|
|
|
|
|
|
|
|
|
|
byte bStart = (byte)(bytes[index] >> skip);
|
|
|
|
|
|
|
|
|
|
var needed = length - (8 - skip);
|
|
|
|
|
if (needed <= 0)
|
|
|
|
|
{
|
|
|
|
|
bStart = (byte)(bStart << (-needed + skip));
|
|
|
|
|
bStart = (byte)(bStart >> (-needed + skip));
|
|
|
|
|
return new[] { bStart };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var needRemain = needed % 8;
|
|
|
|
|
var needCount = needRemain == 0 ? needed / 8 : needed / 8 + 1;
|
|
|
|
|
List<byte> list = new List<byte>(needCount);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= needCount; i++)
|
|
|
|
|
{
|
|
|
|
|
byte b = bytes[index + i];
|
|
|
|
|
list.Add((byte)(((b & ((1 << needRemain) - 1)) << (8 - skip)) | bStart));
|
|
|
|
|
bStart = (byte)(b >> skip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list.Add(bStart);
|
|
|
|
|
|
|
|
|
|
return list.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float Byte2Float(byte[] bytes, int startBit, int length, double factor)
|
|
|
|
|
{
|
|
|
|
|
uint d = Byte2UInt16(bytes, startBit, length);
|
|
|
|
|
return (float)(d * factor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static double Byte2Double(byte[] bytes, int startBit, int length, double factor)
|
|
|
|
|
{
|
|
|
|
|
uint d = Byte2UInt32(bytes, startBit, length);
|
|
|
|
|
return (float)(d * factor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UInt16 Byte2UInt16(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
if (length < 9 || length > 16)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("length should be less then 17 and greater then 8");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] sub = Sub(bytes, startBit, length);
|
|
|
|
|
return BitConverter.ToUInt16(sub, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Int16 Byte2Int16(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
if (length < 9 || length > 16)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("length should be less then 17 and greater then 8");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] sub = Sub(bytes, startBit, length);
|
|
|
|
|
return BitConverter.ToInt16(sub, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static UInt32 Byte2UInt32(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
if (length < 17 || length > 32)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("length should be less then 32 and greater then 17");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] sub = Sub(bytes, startBit, length);
|
|
|
|
|
if (sub.Length < 4)
|
|
|
|
|
{
|
|
|
|
|
sub = new[] { sub[0], sub[1], sub[2], (byte)0 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BitConverter.ToUInt32(sub, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Int32 Byte2Int32(byte[] bytes, int startBit, int length)
|
|
|
|
|
{
|
|
|
|
|
if (length < 17 || length > 32)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("length should be less then 32 and greater then 17");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] sub = Sub(bytes, startBit, length);
|
|
|
|
|
if (sub.Length < 4)
|
|
|
|
|
{
|
|
|
|
|
sub = new[] { sub[0], sub[1], sub[2], (byte)0 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BitConverter.ToInt32(sub, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|