using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Module.Plc.ModbusTcp.Tool
{
///
/// PLC字段解析公共类
///
internal class PlcBitConvert
{
///
/// 无符合16位整形转换为二进制数组
///
///
///
public static bool[] UInt16ToBoolArray(UInt16 val)
{
bool[] array = new bool[16];
byte highByte = (byte)(val / 256);
byte lowByte = (byte)(val % 256);
bool[] lowArray = GetBooleanArray(lowByte);
bool[] highArray = GetBooleanArray(highByte);
for (int i = 0; i < 8; i++)
{
array[i] = lowArray[i];
array[i + 8] = highArray[i];
}
return array;
}
///
/// 字节转换成布尔数组
///
///
///
public static bool[] GetBooleanArray(byte b)
{
bool[] array = new bool[8];
for (int i = 0; i <= 7; i++)
{ //对于byte的每bit进行判定
array[i] = (b & 1) == 1; //判定byte的最后一位是否为1,若为1,则是true;否则是false
b = (byte)(b >> 1); //将byte右移一位
}
return array;
}
}
}