C#C
C#3y ago
54 replies
Valwex

❔ Read bits from a byte[] array

Hi, I was looking for a way to read bits in a byte[] array and advance the position, and I came across the BitStream library (https://github.com/Sewer56/Sewer56.BitStream) because we can't do it natively in C#.
The problem is that I've noticed that I'm not getting the expected behavior in my code
At first, I need to read an int32 in my byte stream, so I did :
var stream = new ArrayByteStream(buffer);
var bitStream = new BitStream<ArrayByteStream>(stream);
var header = bitStream.Read<int>();
Console.WriteLine(header);

And in output I get :
671088640
687865856
704643072
721420288
738197504
754974720
771751936
788529152
805306368
822083584
838860800

Normally I'd get something shorter like this:
67
68
69
70
71
72
73
74
75
76
77
78

I don't want to write a code that will only retrieve the first two characters because I don't necessarily know the size
Do you know why the BitStream library reacts like this? When I use MemoryStream + BinaryReader it works perfectly except that I get stuck when I have to read bits in a stream...

Thanks a lot 🙏
Was this page helpful?