0

I have a binary file that I am trying to extract data from. The last 5 data points int the file are 10 bit integer types and I am struggling on how to extract that information and convert it into something readable. I have tried the following code:

struct bitField
{
    unsigned value: 10;
};

struct Data
{
    bitField x;
}

int main()
{
    std::array<char,696> buffer;
    std::ifstream file ("file.bin", std::ios::in | std::ios::binary);
    file.read(buffer.data(),buffer.size());

    Data a;

    std::memcpy(&a.x.value,&buffer[612],sizeof(struct bitField));

}

I am then met with the error attempt to take address of bit-field. I have then tried using std::bitset<10> in place of bitField in my Data struct. And while I do not get a compiler error, I get back a bunch of 0's instead which I believe is incorrect data.

How do you properly read in the data?

1 Answer 1

1

You can't take an address of a bit-field value as it may not be byte-aligned. You should copy directly into a.x (not a.x.value).

Further, you don't really need to have a separate bitfield struct. You can simply put bitfields right into Data struct.

See this on how to use bitfields: https://www.geeksforgeeks.org/bit-fields-c/

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

1 Comment

I would suggest to use a c++ reference (eg this) instead of a c one (not that it matters much in this case)

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.