Is this a viable BIGENDIAN test:

int tmpvaluethatwillneverreallybeused=1;
bool bigendian=(*(char*)(&tmpvaluethatwillneverreallybeused)==1);
void myFunction()
{
    if (bigendian)
    {
        //do big endian specific code
    }
    else
    {
        //do little-endian specific code
    }
}

Language lawyers can argue a few reasons why that would fail, but in practice it's all but guaranteed to work.

Make sure you are dealing with 32-bit integers. If so, then you can do this.

const int unusedvalue = 1;
const char* ptr2tmp = (char*)&unusedvalue;
const bool bigendian = (1 == ptr2tmp[3]);

Why do it this way, instead of how you showed it? Because it is clearer in what you are doing, and will not take any more processor time. It may, in some situations, be safer as well. Do note, however, that this won't work for C since the initializer of the bool value has to be constant, not an expression like this is. You would have to declare it non-const and compute it after you enter main(). This may be better, and it would work with both C and C++.

int isBigEndian(void)
{
    static const int unusedvalue = 1;
    static const char* ptr2tmp = 0;
    static int bigendian = 0;

    if (ptr2tmp == 0) /* Uninitialized - compute bigendian */
    {
        ptr2tmp = (const char*)&unusedvalue;
        bigendian = (1 == ptr2tmp[3]);
    }
    return bigendian;
}

The value of bigendian is only computed once, and is thereafter easy to determine by simply calling isBigEndian(), plus this code can be used as-is in either a C or C++ program.

Why do it this way, instead of how you showed it? Because it is clearer in what you are doing, and will not take any more processor time. It may, in some situations, be safer as well.

But it only works for 32-bit... Labdabeta's way doesn't depend on the size of an int, only that the size of int and char be different. I'm not convinced that your method is clearer at all, much less sufficiently clearer to justify limiting it to only 32-bit ints at a time where 64-bit is becoming popular.


p.s. Labdabeta's test is reversed. The bigendian variable is set to true for little endian, but that doesn't invalidate the method being used. ;)

commented: GettSize independent code is always better! -- because the alternative is a maintainers nightmare. +9

Re. Deceptikon. True. One could use sizeof(int) to determine where to look, such as ptr2tmp[sizeof(int)-1], which would also work on some embedded systems where sizeof(int) == 2 (16 bits). I just prefer disambiguation to clarify what EXACTLY I am doing. Also, if you are going to do the *(char*)(&unusedvalue) method, as you noted, the original poster's test will be reversed since this looks at the little end of the array. It should be:

int tmpvaluethatwillneverreallybeused=1;
bool bigendian=(*(char*)(&tmpvaluethatwillneverreallybeused)==0);

And finally, my last comment. My point about the code being limited to C++ is still valid. If you want to use this is a C program, then you are SOL and STILL have to place the non-const parts in a function somewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.