• Advertisement

Archived

This topic is now archived and is closed to further replies.

reading binary data with number of bytes

This topic is 5505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have 2 programs. the 1st program reads in a binary file and flips all the data in it.... and writes out a new file with all the reversed bits... like so: FILE *in; FILE *out; #define BYTES_TO_READ (32) while (end of file) { fread(&data,BYTES_TO_READ, 1, in); data = ~data; fwrite(&mp3data,BYTES_TO_READ, 1, out); // write out to file } in the 2nd program, the output file from the 1st program is read in, and all the data is flipped. (essentially having the original file''s data again) however, when it is flipped, the data is only spot-on-correct if it is read with the same number of bytes. (ie BYTES_TO_READ is the same in both programs). This is the problem as the 2nd program that reads in the data and reflips it, will read the data in with different number of bytes (this is determined by something else in code that cannot change), so i need to cater for any number of bytes being used to read back in. the question is.... is there a way i can read & write the data in the 1st program in such a way, that the 2nd program will be able to read and flip the bits with any number of bytes used???? here is an example of the 2nd program code: void ReadFile(unsigned int bytes) { fread(&data,bytes, 1, in); data = ~data; // ''data'' is ready to be used } ... somewhere else in the code, this function is called: ReadFile(6144); ReadFile(10); ReadFile(100); // etc THANKS FOR ANY HELP/ADVICE!!! Paul.

Share this post


Link to post
Share on other sites
Advertisement
You should flip the data, not the pointer.

  
const int DATA_SIZE = 32;
unsigned char data[DATA_SIZE] = { /* Something */ };
for (int i = 0; i < DATA_SIZE; ++i)
{
data[i] = ~data[i];
}




Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''

Share this post


Link to post
Share on other sites

  
void binary_flip(void* data, size_t count)
{
unsigned char* data2 = reinterpret_cast<unsigned char*>(data);
for (size_t i = 0; i < count; ++i)
{
data2[i] = ~data2[i];
}
}

To flip an int do this:
int i = 47;
binary_flip(&i, sizeof(i));





Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''

Share this post


Link to post
Share on other sites
quote:
Original post by bilstonwarrior
unsigned int

quote:
Original post by bilstonwarrior
#define BYTES_TO_READ (32)

A note though, an unsigned int is usually 32-bits on a 32-bit OS, that is 4 bytes. You could use the sizeof operator if you are unsure of the sizes of types.

Share this post


Link to post
Share on other sites
You really should consolidate all your threads on this same problem into one.

That being said, if data is an unsigned int, you can''t read 32 bytes into it. You can only read at most 4. The way you have it now, you''re overwriting other memory, which is all bad.

Share this post


Link to post
Share on other sites
If data is 4 bytes, and you read 32, you''re lucky you don''t crash the program.
Whats happening, is you read 32 bytes in, where theres only supposed to be 4, you do data = ~data which flips all the bits in the int (4 bytes), but since the compiler expects that you''re using 4 bytes, it leaves the other 28 untouched. Then, if you read in 16 bytes in the same way in your other program, the first 4 bytes will be flipped, then the next 12 won''t, then the next time round, then next 16 won''t, then the next time round, the first 4 will, but the rest won''t.


Share this post


Link to post
Share on other sites
right so, if i am reading in a binary file and want to flip all the bytes that are read in, and the bytes read are 4096... how do i flip all the bits...????

i have the following function:


void ReadFile( void *buffer)
{
fread(buffer,4096,1,infile);

// this doesnt work:
binary_flip(&buffer, sizeof(buffer));


}

if i use the "binary_flip" code from one of the answers above, it still doesnt fip everything.

What i need to do if flip all the bytes currently held in "buffer" once it is read in using 4096 bytes.

i have been told that by doing "*(unsigned int *)buffer = ~(*(unsigned int*)buffer);" this is only flipping 1 DWORD.... so how do i flip it all?

thanks

Paul.

Share this post


Link to post
Share on other sites
binary_flip(buffer, 4096);



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''

Share this post


Link to post
Share on other sites

  • Advertisement