S
S
sudo rm -rf /2021-10-19 09:06:49
.NET
sudo rm -rf /, 2021-10-19 09:06:49

How to generate and parse a bitpack in .Net?

There is a task in which it is required to transfer packets over the network.
The packets themselves are a set of bits not aligned to bytes.

For example, here is a packet: 4 bytes of offset, 1 bit - status, 23 bits - latitude, 24 bits - longitude, 12 bits - height, 7 bytes skip, 11 bits - speed relative to the north-south axis, 11 bits - speed relative to the axis West East.

The question arises, how to create such a package using modern .Net and parse it?
If the data were aligned to a multiple of 8, then the same BitArray could be used, but in this situation it swears when you try to call CopyTo() on some byte[] in this situation.
Also, with "flat" data, it would be possible to use SequenceReader and, in principle, System.Buffers, but in SequenceReader it cannot read a given number of bits.

I don't want to use bit masks. Maybe I'm missing something from the available options? How to implement work with such packages?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-10-19
@MaxLevs

I don't want to use bit masks.

And that's the only thing left to use.

R
Ronald McDonald, 2021-10-19
@Zoominger

Mb I’ll say stupidity, but why not collect these bits into bytes (with the missing bits added to the last byte) and overtake these bytes into a string like "00110011" and not pick each bit like a char from a string? And it is easy to form such a packet if the bits are converted to hex and exactly hexes are transmitted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question