How can i find out if machine is big/little endian with C... i know big/little endian but dont know how to write program for it. I dont want code but just point me in right direction.
felipe
How can i find out if machine is big/little endian with C... i know big/little endian but dont know how to write program for it. I dont want code but just point me in right direction.
felipe
Since endianess works on a byte-level, that means anything larger than one byte will be affected.
So create an integer of >= 2 bytes, fill it with a known value, then check its bytes one by one. Depending on the endianess, they will be appear in different order.
By "checking its bytes" as Elysia suggested, it should be accessed one byte at a time after casting an integer to a byte array and looking at those elements. Or using a union to access the data type as integer or bytes.
We would 'and' the variable against a known value (something with the high or low bits set e.g. 0x000F) and evaluate the result.
As nonoob explained you can check the bytes of an integer by casting it to a char*.
Now p[0] will be the first byte in the representation of data, and p[1] will be the second. If p[0] is 0x20 the machine is little endian, for example.Code:short data = 0x1020; char *p = (char *)&data;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
gracias a todos and specially to dwks.
Sorry, just read the part about not wanting any code.