protected BinaryCodeBuilder EncodeLength(uint length) { byte lengthSizeInBytes = GetNumberOfBytesToStoreLength(length); BinaryCodeBuilder builder = new BinaryCodeBuilder(); builder.AppendBit((byte)(lengthSizeInBytes % 2)); builder.AppendBit((byte)((lengthSizeInBytes / 2) % 2)); // Minimum length is 1 byte so we start counting that value with 0, meaning 0 represents 1, 1 represents 2 etc. byte[] lengthBytes = BitConverter.GetBytes(length); for (int i = 0; i <= lengthSizeInBytes; i++) { builder.AppendByte(lengthBytes[i]); } return(builder); }
public void TestBytesAdd() { var builder1 = new BinaryCodeBuilder(); var builder2 = new BinaryCodeBuilder(); var bytes = new byte[] { 100, 140, 255, 0, 24 }; for (int i = 0; i < bytes.Length; i++) { builder1.AppendByte(bytes[i]); } builder2.AppendBytes(bytes); var storedBytes1 = builder1.ToBytes(); var storedBytes2 = builder2.ToBytes(); for (int i = 0; i < bytes.Length; i++) { Assert.AreEqual(bytes[i], storedBytes1[i]); Assert.AreEqual(bytes[i], storedBytes2[i]); } }