Esempio n. 1
0
        public static BitString To16BitString(short value)
        {
            var bytesInLSB    = BitConverter.GetBytes(value);
            var bitArrayInLsb = Helper.LeastSignificantByteArrayToLeastSignificantBitArray(bytesInLSB);

            return(new BitString(bitArrayInLsb));
        }
Esempio n. 2
0
        public static BitString To64BitString(ulong value)
        {
            var bytesInLSB  = BitConverter.GetBytes(value);
            var bitArrayLSb = Helper.LeastSignificantByteArrayToLeastSignificantBitArray(bytesInLSB);

            return(new BitString(bitArrayLSb));
        }
Esempio n. 3
0
        /// <summary>
        /// Create a <see cref="BitString"/> using MSB hex.
        /// </summary>
        /// <param name="hexMSB">The MSB hexadecimal string</param>
        /// <param name="bitLength">The length of the resulting <see cref="BitString"/> by taking that amount of MSBs</param>
        /// <param name="truncateBitsFromEndOfLastByte">When the bitLength is not a multiple of 8, the hex needs to be truncated in the last byte. This parameter determines which side of the last byte is truncated</param>
        public BitString(string hexMSB, int bitLength = -1, bool truncateBitsFromEndOfLastByte = true)
        {
            if (string.IsNullOrEmpty(hexMSB) || bitLength == 0)
            {
                _bits = new BitArray(0);
                return;
            }

            hexMSB = hexMSB.Replace(" ", "");
            int numberChars = hexMSB.Length;

            if (numberChars % 2 != 0)
            {
                throw new InvalidBitStringLengthException($"{nameof(BitString)}s are expected to have an even number of hex characters. Value was \"{hexMSB}\".");
            }

            byte[] bytesInMSB = new byte[numberChars / 2];
            for (int i = 0; i < numberChars; i += 2)
            {
                bytesInMSB[i / 2] = Convert.ToByte(hexMSB.Substring(i, 2), 16);
            }

            if (bitLength < 0)
            {
                _bits = Helper.MostSignificantByteArrayToLeastSignificantBitArray(bytesInMSB);
            }
            else
            {
                var bitsNeeded = System.Math.Min(bitLength, numberChars * BITSINBYTE / 2);

                var bitsInMSB     = Helper.MostSignificantByteArrayToMostSignificantBitArray(bytesInMSB);
                var truncatedBits = new BitArray(bitsNeeded);

                if (truncateBitsFromEndOfLastByte)
                {
                    for (var i = 0; i < bitsNeeded; i++)
                    {
                        truncatedBits[i] = bitsInMSB[i];
                    }
                }
                else
                {
                    var firstBits = bitsNeeded - (bitsNeeded % 8);
                    for (var i = 0; i < firstBits; i++)
                    {
                        truncatedBits[i] = bitsInMSB[i];
                    }

                    var skippedBits = (bytesInMSB.Length * BITSINBYTE) - bitsNeeded;
                    for (var i = firstBits; i < bitsNeeded; i++)
                    {
                        truncatedBits[i] = bitsInMSB[i + skippedBits];
                    }
                }

                _bits = Helper.ReverseBitArrayBits(truncatedBits);
            }
        }
Esempio n. 4
0
        public BitString(byte[] bytesToConcatenate, int bitLengthToHit)
        {
            var maxBytes = bitLengthToHit.CeilingDivide(BITSINBYTE);
            var bytes    = new byte[maxBytes];

            for (var i = 0; i < maxBytes; i++)
            {
                bytes[i] = bytesToConcatenate[i % bytesToConcatenate.Length];
            }

            var shortenedBits = Helper.MostSignificantByteArrayToLeastSignificantBitArray(bytes);

            if (shortenedBits.Length > bitLengthToHit)
            {
                shortenedBits = shortenedBits.SubArray(0, bitLengthToHit);
            }

            _bits = shortenedBits;
        }
Esempio n. 5
0
 public static BitArray Reverse(this BitArray bArray)
 {
     return(MsbLsbConversionHelpers.ReverseBitArrayBits(bArray));
 }
Esempio n. 6
0
 /// <summary>
 /// Create <see cref="BitString"/> expecting <see cref="byte[]"/> in Most Significant Byte (MSB) order.
 /// </summary>
 /// <param name="msBytes">The MSB bytes to use in the LSb <see cref="BitString"/></param>
 public BitString(byte[] msBytes)
 {
     _bits = Helper.MostSignificantByteArrayToLeastSignificantBitArray(msBytes);
 }