C / C++ FAQs & Programming Resources - ProkutFAQ : FindEndian

HomePage Recent Changes Recently Commented Login/Register

How to find the endianness of a system?


Finding the byte order(byte-endianness):
Courtesy: Alessandro's post in C community.
    int nNumber = 1;
    char cArray[ sizeof(int) ];
   
    memcpy(cArray, &nNumber, sizeof(int));
    if (cArray[ 0 ] == 0x01)
    return IS_LITTLE_ENDIAN;
    else
    return IS_BIG_ENDIAN;


Note: This would be appropriate only for those systems which have either little or big endian. But if the system is middle-endian, this code would produce meaningless results.



 Comments [Hide comments/form]
See an excellent explanation here..
http://cracktheinterview.in/viewtopic.php?f=1&t=28

Cheers,
aD
-- triband-del-59.180.8.221.bol.net.in (2008-01-19 04:09:26)
Page was generated in 0.0402 seconds