© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
58 replies
Determinism

Converting ReadOnlySequence of bytes to object

I have to convert a
ReadOnlySequence<byte>
ReadOnlySequence<byte>
to an object, the way I'm doing it currently is like so:
    public static DataModel ConvertBytes(ReadOnlySequence<byte> b)
    {
        var msg = new DataModel();

        int location = 8;

        msg.FFOn = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.FFSel = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.On = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.Sel = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.On = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.Sel = BitConverter.ToInt32(b, location) != 0; location += 4;

        // ...
        
        location += 76 * 4;

        msg.ZoneShapeTarget01 = BitConverter.ToSingle(b, location); location += 4;
        msg.ZoneShapeTarget02 = BitConverter.ToSingle(b, location); location += 4;
    }
    public static DataModel ConvertBytes(ReadOnlySequence<byte> b)
    {
        var msg = new DataModel();

        int location = 8;

        msg.FFOn = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.FFSel = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.On = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.Sel = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.On = BitConverter.ToInt32(b, location) != 0; location += 4;
        msg.Sel = BitConverter.ToInt32(b, location) != 0; location += 4;

        // ...
        
        location += 76 * 4;

        msg.ZoneShapeTarget01 = BitConverter.ToSingle(b, location); location += 4;
        msg.ZoneShapeTarget02 = BitConverter.ToSingle(b, location); location += 4;
    }

Is there a better way to handle this? BitConverter doesn't support ReadOnlySequence, so I would have to convert it to a byte array which is not ideal. I also have tons of properties on this model so it is a ton of code. Thanks in advance!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Replace Bytes To Another Bytes
C#CC# / help
2y ago
❔ Help needed converting anon object to JSON
C#CC# / help
3y ago
❔ Bytes to UInt64
C#CC# / help
3y ago