   Converting 4 bytes of an istream to a float?
  Hi,hope someone out there can give me a hand...I'm just in the process of writing my first non-tutorial c++ program.  In this program, I'm tring to read in a file with IFF syntax.  I've approached this by reading it in one char at at time using the istream get function.  I've been able to convert 4 byte chunks to longs using bitwise operators, but am totally stumped when it comes to floats from 4 byte chunks.  Can't seem to find any code in any of my books that suggests how to do it.Anyone got any advice?Thanks,Jaspar   
  I would use:atof()From the cstdlib.Dave   ;  I hesitate to post this, as it's probably bad practice, but:float value = *(reinterpret_cast<float*>(data));Don't forget to account for endian-ess.There are probably better methods than this (such as using a third-party serialization library), but this method will work.   ;  istream has an overloaded operator>> for floats, so you can do:somestream >> somefloat; // reads one float from the stream;  Um, isn't IFF a binary file type?  I don't see how atof(ascii to float) really has anything to do with this.  You probably should be using binary file i/o and not get, however not being familiar with the file format the float may not be stored correctly.  To do it with bitwise operations, try this:char data[4]; // stores the 4 bytes you're going to read in// get the data from file hereunsigned int iff_int; // or whatever your 4 byte data type is, maybe long...iff_int = data[0] | data[1] << 1 | data[2] << 2 | data[3] << 3;float iff_float = *(float*)(&iff_int); // this takes the address of iff_int, converts it to a float pointer, then dereferances the// float pointer and assigns that to iff_floatthe line iff_int = ... is where you'd have to fiddle around with to get the data loaded correctly.  It will probably either be in the order it is now or the opposite order.The better way to do it would be binary file i/o.  To do that do this:fstream file("file.iff", ios::binary | ios::in); // open file for inputint iff_int;file.read((char*)&iff_int, sizeof(int)); // reads sizeof(int) bytes and stores it into iff_int.  position in file is sizeof(int) from startfloat iff_float;file.read((char*)iff_float, sizeof(float); // reads sizeof(float) bytes and stores it into iff_float.  position is sizeof(int)+sizeof(float) from start of fileThe problem with this is the data must be stored correctly.  For a decent tutorial on file i/o(binary file i/o is in the 2nd half) try this out:http://www.cplusplus.com/doc/tutorial/files.htmlHope this helps   ;Quote:Original post by nobodynews...snippet removed...The file io method is better, but I just thought I'd mention that technically you don't have to convert the data to an int before converting to float; you can just reinterpret cast the data pointer directly as in my earlier example.   ;  If the file is using native endianness, just use the ">> (float)" operator for the stream.  If it's reverse endianness, then read the bytes backwards into an int and then do this:int helperInt;// read one byte at a time into helperIntfloat finalResult = *(float*)(&helperInt);Basically, a float takes up 4 bytes, and so does an int.  So all you have to do is convince C that the bytes that you read in should be used as a float.I haven't examined all of the streams, but they might have streams written with endianness settings.   ;  Thanks everybody!I reckon nobodynews has summed up the solution to my problem perfectly, will try it all out in due course.  Thanks for all the other tips everyone, this forum's a wonderful source of info!Jaspar   