You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace Module.Plc.ModbusTcp.Tool
{
/// <summary>
/// PLC字段解析公共类
/// </summary>
internal class PlcBitConvert
{
/// <summary>
/// 无符合16位整形转换为二进制数组
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
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 ;
}
/// <summary>
/// 字节转换成布尔数组
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
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 ;
}
}
}