Пример #1
0
        /// <summary>
        /// Gets/Sets the byte at index specified.
        /// Index 0 is the most significant byte.
        /// </summary>
        /// <param name="index">The index to get/set (index 0 is most significant byte)</param>
        /// <returns></returns>
        public byte this[int index]
        {
            get
            {
                // Get bits for index (bits are in LSb, whereas bytes as MSB).
                // So byte index 0, is the last 8 bits of the BitArray
                BitString bits = this.Substring(BitLength - ((index + 1) * 8), 8);

                return(bits.ToBytes().FirstOrDefault());
            }
            set
            {
                // Put the single byte in a byte array
                byte[] byteArray = new byte[1] {
                    value
                };
                // convert that byte array to a bit array
                BitArray bits = new BitArray(byteArray);

                // For each bit, set that bit in this,
                // noting that bits and bytes are in opposite endianness.
                for (int i = 0; i < bits.Length; i++)
                {
                    var bitToSet = BitLength - ((index + 1) * 8) + i;
                    this.Set(bitToSet, bits[i]);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Only for use in SHA3
        /// </summary>
        /// <returns>Normal hex, but the last byte is truncated to match little endian format</returns>
        public string ToLittleEndianHex()
        {
            if (BitLength == 0)
            {
                return("00");
            }

            var bytes = new byte[] { };

            // Make a padded BitString if the length isn't % 8
            if (BitLength % BITSINBYTE != 0)
            {
                var padding   = BITSINBYTE - BitLength % BITSINBYTE;
                var extraBits = BitLength % BITSINBYTE;
                var lastBits  = this.Substring(0, extraBits);

                var paddedBS = new BitString(0);
                if (BitLength < BITSINBYTE)
                {
                    paddedBS = BitString.Zeroes(padding);
                    paddedBS = BitString.ConcatenateBits(paddedBS, lastBits);
                }
                else
                {
                    var firstBits = this.Substring(extraBits, BitLength - extraBits);
                    paddedBS = BitString.ConcatenateBits(paddedBS, firstBits);

                    paddedBS = BitString.ConcatenateBits(paddedBS, BitString.Zeroes(padding));
                    paddedBS = BitString.ConcatenateBits(paddedBS, lastBits);
                }

                bytes = paddedBS.ToBytes();
            }
            else
            {
                bytes = ToBytes();
            }

            StringBuilder hex = new StringBuilder(bytes.Length * 2);

            for (int index = 0; index < bytes.Length; index++)
            {
                hex.AppendFormat("{0:x2}", bytes[index]);
            }

            return(hex.ToString().ToUpper());
        }
Пример #3
0
        /// <summary>
        /// Shifts bits in the LSB direction. Shift adds 0s to the end.
        /// </summary>
        /// <remarks>Keeps output length same as input.</remarks>
        /// <param name="bStr">THe bitstring to shift</param>
        /// <returns></returns>
        public static BitString LSBShift(BitString bStr)
        {
            var block  = bStr.ToBytes();
            var output = new byte[block.Length];

            int  i   = block.Length;
            uint bit = 0;

            while (--i >= 0)
            {
                uint b = block[i];
                output[i] = (byte)((b << 1) | bit);
                bit       = (b >> 7) & 1;
            }

            return(new BitString(output).GetLeastSignificantBits(bStr.BitLength));
        }
Пример #4
0
        public string ToHex()
        {
            if (BitLength == 0)
            {
                return("");
            }

            var bytes = new byte[] { };

            // Make a padded BitString if the length isn't % 8
            if (BitLength % 8 != 0)
            {
                var padding  = BITSINBYTE - BitLength % BITSINBYTE;
                var paddedBS = new BitString(BitLength + padding);

                for (var i = 0; i < BitLength; i++)
                {
                    paddedBS.Set(i + padding, Bits[i]);
                }

                bytes = paddedBS.ToBytes();
            }
            else
            {
                bytes = ToBytes();
            }

            StringBuilder hex = new StringBuilder(bytes.Length * 2);

            for (int index = 0; index < bytes.Length; index++)
            {
                hex.AppendFormat("{0:x2}", bytes[index]);
            }

            return(hex.ToString().ToUpper());
        }
Пример #5
0
 public static BitString ReverseByteOrder(BitString input)
 {
     return(new BitString(MsbLsbConversionHelpers.ReverseByteOrder(input.ToBytes())));
 }