32-bit real binary read

Hello people,

I'm trying to read a file with the following specifications:

stored as 32 bit real (2048x2048 pixel)
6 bytes offset (header)
Little-Endian Byte order.

It's actually a grayscale image, but stored in 32-bit real binary. I've succeeded in viewing the contents of the image using the program ImageJ, but what I'm failing with is converting this data to some integer grayscale image, the program I'm using is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

char y;
unsigned long int z;
unsigned long int i;
int main(int argc, char** argv)
{
    fstream openfile("Bent19.DMP",ios::in | ios::binary);
    fstream writefile("Test.txt",ios::out);
    for(z=0;z<6;z++)
        openfile >> y;
    while(!openfile.eof())
    {

        z=0;
        openfile >> y;
        z=z+(((unsigned short)y)-65280)*pow(2,0);

        openfile >> y;
        z=z+(((unsigned short)y)-65280)*pow(2,8);

        openfile >> y;
        z=z+(((unsigned short)y)-65280)*pow(2,16);

        openfile >> y;
        z=z+(((unsigned short)y)-65280)*pow(2,24);

        writefile << z << endl;
    }
    return (EXIT_SUCCESS);
}


What I'm doing here is that I'm reading the bytes 1 by 1, and then add every 4 read bytes to 1 unsigned long integer. Every time I read a byte I cast it as a short integer (2 bytes) and then remove the extra byte by subtracting 65280 from it. I'm using this sequence because the file is Little-endian.

This doesn't work and doesn't convert the way I want it, can someone please explain what my mistake is? or give me some alternative program?

Thank you for reading :-), any comment is highly appreciates :-)
Use the bitshift operators to simplify things.
1
2
3
4
z += y;
z += y << 8;
z += y << 16;
z += y << 24;
Last edited on
Thank you pal for the information ;-).

Any other comments? :-)
Topic archived. No new replies allowed.