how convert 4 bytes string to integer?

How to convert 4 bytes n read from binary file to integer header_offset?

1
2
3
4
5
6
 uint32_t header_offset ;   char n[4]; size_t in;
  ...
  in = fread(n, 4, 1, fp); // value n is "\000\000\000\020"
  if (!in)
    return -2;
  header_offset = n; // get 4 bytes to integer 


The header_offset is position of bitmap in BMP file.

Maybe if there would be a way how to copy byte by byte from left to right - 4 bits on one cycle, I could read the 4 bytes in loop char by char.

Last edited on
Do you want something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
{
	char n[] = {'\000', '\000', '\000', '\020'};
	unsigned header_offset = 0;
	int i = 0;
	for (; i < 3; ++i)
	{
		header_offset |= n[i];
		header_offset <<= 8;
	}
	
	header_offset |= n[i];
	cout << header_offset << endl;
}
Yes, thank you
Topic archived. No new replies allowed.