using System; using System.Collections; namespace S7.Net.Types { /// /// Contains the conversion methods to convert Bit from S7 plc to C#. /// public static class Bit { /// /// Converts a Bit to bool /// public static bool FromByte(byte v, byte bitAdr) { return (((int)v & (1 << bitAdr)) != 0); } /// /// Converts an array of bytes to a BitArray. /// /// The bytes to convert. /// A BitArray with the same number of bits and equal values as . public static BitArray ToBitArray(byte[] bytes) => ToBitArray(bytes, bytes.Length * 8); /// /// Converts an array of bytes to a BitArray. /// /// The bytes to convert. /// The number of bits to return. /// A BitArray with bits. public static BitArray ToBitArray(byte[] bytes, int length) { if (length > bytes.Length * 8) throw new ArgumentException($"Not enough data in bytes to return {length} bits.", nameof(bytes)); var bitArr = new BitArray(bytes); var bools = new bool[length]; for (var i = 0; i < length; i++) bools[i] = bitArr[i]; return new BitArray(bools); } } }