Help - how to check if an embbeded Linux uses Llittle-Endian or Big-Endian - Linux

This is a discussion on Help - how to check if an embbeded Linux uses Llittle-Endian or Big-Endian - Linux ; Hi, Just wondering if Linux has a command or something we can use to check if it uses Little-Endian or Big-Endian. This is an embedded Linux running on MIPS. Thanks, TJ --...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Help - how to check if an embbeded Linux uses Llittle-Endian or Big-Endian

  1. Help - how to check if an embbeded Linux uses Llittle-Endian or Big-Endian

    Hi,

    Just wondering if Linux has a command or something we can use to check if it
    uses Little-Endian or Big-Endian. This is an embedded Linux running on
    MIPS.

    Thanks,


    TJ
    --




  2. Re: Help - how to check if an embbeded Linux uses Llittle-Endian or Big-Endian

    On Mon, 24 Jul 2006 11:50:41 -0500, TJ wrote:

    > Hi,
    >
    > Just wondering if Linux has a command or something we can use to check if it
    > uses Little-Endian or Big-Endian. This is an embedded Linux running on
    > MIPS.


    #include
    #include

    int main() {
    if (htons(1) == 1) puts("big endian");
    else puts("little endian");
    return 0;
    }

    Works on any *nix ;-)

    Greetings,
    Aaron
    --
    Aaron Isotton | http://www.isotton.com/
    Black holes are where God divided by zero. -- Steven Wright


  3. Re: Help - how to check if an embbeded Linux uses Llittle-Endian or Big-Endian

    Aaron Isotton wrote:

    > On Mon, 24 Jul 2006 11:50:41 -0500, TJ wrote:
    >
    >>Hi,
    >>
    >>Just wondering if Linux has a command or something we can use to check if it
    >>uses Little-Endian or Big-Endian. This is an embedded Linux running on
    >>MIPS.

    >
    > #include
    > #include
    >
    > int main() {
    > if (htons(1) == 1) puts("big endian");
    > else puts("little endian");
    > return 0;
    > }
    >
    > Works on any *nix ;-)


    Found this hack method[1] via a bug in a program that I wrote always [seemed
    to have] worked[2] until it went onto a specific machine - that machine
    happened to be big-endian and all the other machines I had tested the
    program on had been little-endian):

    int is_big_endian ()
    {
    long int test = 1;
    return !(*((char *)&test));
    }

    you can use the above function, eg:

    ....
    if (is_big_endian()) puts("Big-endian");
    else puts("Little-endian");

    [1] To work it relies on char being 8 bit, or possibly 16 bit, and long int
    being > char (pref/poss 32 bit or bigger).
    [2] obviously I was lucky[3] that it had worked;
    [3] even more lucky was I was reading from, and not writing to, memory.


+ Reply to Thread