Recently I was (again) reading about 'endian'ness. I know how to identify the endianness of host, as there are lots of post on SO, and also I have seen this, which I think is pretty good resource.
However, one thing I like to know is to how to detect the endianness of input binary file. For example, I am reading a binary file (using C++
) like following:
ifstream mydata("mydata.raw", ios::binary);
short value;
char buf[sizeof(short)];
int dataCount = 0;
short myDataMat[DATA_DIMENSION][DATA_DIMENSION];
while (mydata.read(reinterpret_cast<char*>(&buf), sizeof(buf)))
{
memcpy(&value, buf, sizeof(value));
myDataMat[dataCount / DATA_DIMENSION][dataCount%DATA_DIMENSION] = value;
dataCount++;
}
I like to know how I can detect the endianness in the mydata.raw
, and whether endianness affects this program anyway.
Additional Information:
- I am only manipulating the data in
myDataMat
using mathematical operations, and no pointer operation or bitwise operation is done on the data). - My machine (host) is little endian.
htons
etc when writing data to the file andntohs
etc when reading it – Cordiality0x2a00
, you cannot determine if it is42
(little endian) or10752
(big endian). – Tagore