jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
289 stars 62 forks source link

Bug: FieldOrderAttribute isn't respected within Byte #138

Closed Texnomic closed 5 years ago

Texnomic commented 5 years ago

The following Test Case produces 10000000 while it should produce 00000001.

namespace Texnomic.Tests
{
    [TestClass]
    public class FieldOrder
    {
        public class TestObject
        {
            [FieldOrder(0), FieldBitLength(7)] 
            public byte Value { get; set; }

            [FieldOrder(1), FieldBitLength(1)] 
            public bool Flag { get; set; }
        }

        [TestMethod]
        public void Run()
        {
            var TestObject = new TestObject()
            {
                Flag = true
            };

            var Serializer = new BinarySerializer();

            var Result = Serializer.Serialize(TestObject);

            var Byte = Convert.ToString(Result[0], 2).PadLeft(8, '0');

            Assert.AreEqual("00000001", Byte);
        }
    }
}
jefffhaynes commented 5 years ago

That's actually correct. Field order refers to the field order, not the byte order. There is currently no way to achieve what you want without reversing the order of the fields. I've considered adding a "bit-numbering" attribute but since there is a workaround, I haven't spent much time on it.