An external group provides me with a file written on a Big Endian machine, and they also provide a C++ parser for the file format.
I only can run the parser on a lit
It depends upon what you are doing with the data. If you are going to print the data out, you need to swap the bytes on all the numbers. If you are looking through the file for one or more values, it may be faster to byte swap your comparison value.
In general, Greg is correct, you'll have to do it the hard way.
Your question somehow conatins the answer: No!
I only can run the parser on a little endian machine - is there any way to read the file using their parser without add a swapbytes() call after each read?
If you read (and want to interpret) big endian data on a little endian machine, you must somehow and somewhere convert the data. You might do this after each read or after the whole file has been read (if the data read does not contain any information on how to read further data) - but there is no way in omitting the conversion.
You could write a parser that wraps their parser and reverses the bytes, if you don't want to modify their parser.
Be conscious of the types of data being read in. A 4-byte int
or float
would need endian correction. A 4-byte ASCII string would not.
Try persuading the parser team to include the following code:
int getInt(char* bytes, int num) { int ret; assert(num == 4); ret = bytes[0] << 24; ret |= bytes[1] << 16; ret |= bytes[2] << 8; ret |= bytes[3]; return ret; }
it might be more time consuming than a general int i = *(reinterpret_cast<*int>(&myCharArray));
but will always get the endianness right on both big and small endian systems.
In general, no.
If the read/write calls are not type aware (which, for example fread and fwrite are not) then they can't tell the difference between writing endian sensitive data and endian insensitive data.
Depending on how the parser is structured you may be able to avoid some suffering, if the I/O functions they use are aware of the types being read/written then you could modify those routines apply the correct endian conversions.
If you do have to modify all the read/write calls then creating just such a routine would be a sensible course of action.
In general, there's no "easy" solution to this. You will have to modify the parser to swap the bytes of each and every integer read from the file.