Good day to all, help me figure out how to read a bit field from a binary file? There is a structure written in C ++ Builder:

typedef struct { unsigned int nr:8; // unsigned int y:8; // 1 unsigned int sm:3; // 2 unsigned int d:5; // 3 unsigned int m:4; // 4 unsigned int r:4; // 5 } n_def; 

As I understand it, I first need to read the entire block and then break it into bits?

 using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open))) { // пока не достигнут конец файла // считываем каждое значение из файла while (reader.PeekChar() > -1) { int numb = reader.ReadInt32(); uint nr = reader.ReadUInt32(); // переменная которую нужно разбить на биты } } 

In the second field, nr I already get not what I need. How to properly implement the task? can it be better to use another way of reading and writing to a binary file? thank you for your responses

Continuing the theme: Binary file , forgive me for posting on the exchanger just the file contains data from 2004.

C ++ Builder

And in VS the data is: nr = 1

y = 16 (matched)

sm = 2

ds = 15

ms = 11

rs = 0

    1 answer 1

    In theory, it makes sense to use the BitVector32 class. Take the SectionHelper helper class from here .

    You need to prepare a description:

     SectionHelper helper = new SectionHelper(); var nr_s = helper.AllocatedSection(8); var y_s = helper.AllocatedSection(8); var sm_s = helper.AllocatedSection(3); var d_s = helper.AllocatedSection(5); var m_s = helper.AllocatedSection(4); var r_s = helper.AllocatedSection(4); 

    Now you can enjoy.

     Int32 ndef = reader.ReadInt32(); BitVector32 bv = new BitVector32(ndef); int nr = bv[nr_s]; int y = bv[y_s]; int sm = bv[sm_s]; // ... 

    On the record:

     BitVector32 bv = new BitVector32(ndef); bv[nr_s] = nr; bv[y_s] = y; bv[sm_s] = sm; // ... Int32 ndef = bv.Data; // и записываете ndef куда надо 
    • Thank you so much, but on the record, will everything look exactly the same? - Ethernets
    • one
      @Ethernets: Please! Finished the answer. - VladD
    • Thank you so much - Ethernets
    • @Ethernets: Please! - VladD
    • excuse me, but I found a data discrepancy between what is in C ++ and what I wrote according to your example, I only had 1 number matched under the variable y Could this be due to the type mismatch? In my question in C ++, the unsigned int type is used, but in your int, this is not the same thing, or do I not understand something? help please understand. Thank you - Ethernets