Mail Archives: djgpp/2000/02/24/06:38:17
On 23 Feb 2000, Dieter Buerssner wrote:
> The only problem may arise from the endianess of the format.
> If you have to swap endianess, the following code snippet may
> work. (untested)
>
> FILE *fp;
> unsigned char rawdata[4];
> unsigned char t;
> float x;
>
> fp = fopen("binfloat.dat", "rb");
> if (fp)
> {
> if (fread(rawdata, sizeof rawdata, 1, fp) == 1)
> {
> /* swap endianess */
> t = rawdata[0]; rawdata[0] = rawdata[3]; rawdata[3] = t;
> t = rawdata[1]; rawdata[1] = rawdata[2]; rawdata[2] = t;
> x = *(float *)rawdata; /* x is the float you wanted */
> }
> }
There are library functions (htons, ntohl, etc.) to do this more
portably.
- Raw text -