0

I'm new here so I'll try to be very clear with my issue. I've tried to get a direct answer, but when I check on other questions, they are very particular and I get confused.

I have a binary file and I need to read it for my project. I also have an specification sheet, and I'm reading the file accordingly to those specs. So I've created a cpp file, and writing a simple program to read each element. I use ifstream, and read() functions to read from file.

The problem is when on the specification sheet, I get that I need to read a bitstring with size 12. From the details, it's very clear that I should read only 12 bits for each of this elements. But I'm not really sure if reading bit to bit is possible. Rest of elements were read in bytes. And also, If I read 2 bytes each time and use bit "masks" to get 12 bits only, the rest of elements read after this does not match correctly. So my guess is that I really need to read only 12 bits.

So my question. Is it possible to read 12 bits from a binary file? or reading Bit to bit? . And I mean only 12, without reading bytes and then masking them.

Thanks a lot.

5
  • Can you post the actual spec where it says you need to read in 12 bit blocks? I can see them specifying 12 bytes but not bits. Commented Aug 10, 2015 at 12:08
  • @NathanOliver: several communication protocols have fields where bit length is not a multitude of 8. See IP header Commented Aug 10, 2015 at 12:20
  • @stefaanv yes but the entire header is still in multiples of 8. Commented Aug 10, 2015 at 12:23
  • @NathanOliver: still, it is nice if you can read in field per field, which is how I understood the question. Commented Aug 10, 2015 at 12:26
  • Thanks everyone. As I can see in the answers, I too point at the specification sheet. I only have a printed copy at the moment but I'm going to ask directly to the ones that created it. Seems like something is not matching right....since there is only 1 element which requires non byte multiple values. Commented Aug 11, 2015 at 10:49

2 Answers 2

4

No, this is not possible.

What you should do is read 2 bytes, mask 12 bits to get the result you want but also store the other 4 bits somewhere. Now when you need 12 bits again, read only 1 byte and combine it with the 4 stored bits.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming little endian.

  1. read file to an array of uint8_t that is padded to a multiple of 6 bytes

  2. make your access function

    uint16_t get12Bits(uint8_t *ptr, int loc)
    {
        uint64_t temp;// use lower 48 bits
        memcpy(&temp, ptr+(loc&~0x03), 6*uint8_t);//6bytes, 4 elements
        return 0xfff&(temp>>(loc&0x03)*12);
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.