I have a binary file that contains n single-precision values. I know that the format used when writing data was big-endian. When I read in the data into a float vector ('mainvector' in the code below), by default, the data is read in according to little-endian format. I use the following to read data:
ifstream inputfile("filepath",ifstream::in|ifstream::binary)
inputfile.read(reinterpret_cast<char*>(&mainvector[0]), n*4);
inputfile.close()
There is a lot of discussion on endianness and conversion on stackoverflow itself. However, this is the first time I have to deal with the endianness issue and all the information available is a bit overwhelming. In the process I learned that the bytes are reversed (big vs. little endian).
Is there a one-liner that I can incorporate to change the default little-endian treatment of my binary data to big-endian, or post-process my mainvector in order to get the original data ? or would I need to manually reverse byte-order of each of the n values ?
UPDATE: It appears that there is no one-liner for this. Individually changing the byte order is the way to do it! How to do that is discussed here by Mats and elsewhere (e.g. here).