public void ReverseBitArrayBitsShouldReturnBitArrayInReverseOrder(string label, bool[] bits) { var ba = new BitArray(bits); var subject = MsbLsbConversionHelpers.ReverseBitArrayBits(ba); for (int i = 0; i < subject.Length; i++) { Assert.AreEqual(ba[i], subject[subject.Length - 1 - i]); } }
private static BitString ConvertEndianness(BitString message) { // For a small example... 60 01 (hex) = 0110 0000 0000 0001 (binary) // should turn into 06 80 (hex) = 0000 0110 1000 0000 (binary var messageLen = message.BitLength; var reversedBits = MsbLsbConversionHelpers.ReverseBitArrayBits(message.Bits); var normalizedBits = MsbLsbConversionHelpers.ReverseByteOrder(new BitString(reversedBits).ToBytes()); message = new BitString(normalizedBits); var hex = message.ToHex(); message = new BitString(hex, messageLen, false); return(message); }
protected BitString Convert168BitKeyTo192BitKey(BitString origKey) { if (origKey.BitLength != 168) { throw new ArgumentException("Not a valid key, needs 168 bits"); } var bsReverse = new BitString(MsbLsbConversionHelpers.ReverseBitArrayBits(origKey.Bits)); var newBs = new BitString(0); for (var i = 0; i < bsReverse.BitLength; i++) { newBs = newBs.ConcatenateBits(new BitString(new BitArray(new[] { bsReverse.Bits[i] }))); if ((i + 1) % 7 == 0) { newBs = newBs.ConcatenateBits(BitString.Zero()); } } return(newBs); }
private static BitString ConvertEndianness(BitString message) { // This is kinda gross... The message input is in the correct byte order but reversed bit order // So we must reverse the bits, then reverse the bytes to put everything in the correct order // // For a small example... 60 01 (hex) = 0110 0000 0000 0001 (binary) // should turn into 06 80 (hex) = 0000 0110 1000 0000 (binary var messageLen = message.BitLength; // Convert to big endian byte order but little endian bit order var reversedBits = MsbLsbConversionHelpers.ReverseBitArrayBits(message.Bits); var normalizedBits = MsbLsbConversionHelpers.ReverseByteOrder(new BitString(reversedBits).ToBytes()); // After the byte conversion make sure the result is the correct length // The constructor here handles this for us message = new BitString(normalizedBits); var hex = message.ToHex(); message = new BitString(hex, messageLen, false); return(message); }