Endianness programme help

This is a discussion on Endianness programme help within the C Programming forums, part of the General Programming Boards category; Hello Everyone, How can i write the c code to check whether my machine is little indian or big indian???? ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Unhappy Endianness programme help

    Hello Everyone,

    How can i write the c code to check whether my machine is little indian or big indian????
    and how to convert one from other?? Please explain me the logic????????/
    Last edited by rits; 03-12-2009 at 09:42 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int x = 1;
    if (*(char *)&x)
        // Little endian
    else
        // Big endian
    It works by reading the lowest byte (char) of the variable x. If it's non zero, then the machine is little endian (first byte in memory is the 0x01 byte of 0x00000001).

    --
    Mats
    Last edited by matsp; 03-12-2009 at 09:56 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm sure Mats will edit the & into his example here shortly.

    As to converting, you just reassemble the bytes in the opposite order (and you can peel off the bytes using pointers as above). Generally you only do this if you need to communicate between machines, and if so, then using something like htons and friends would be the Right Thing to do.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    I'm sure Mats will edit the & into his example here shortly.

    As to converting, you just reassemble the bytes in the opposite order (and you can peel off the bytes using pointers as above). Generally you only do this if you need to communicate between machines, and if so, then using something like htons and friends would be the Right Thing to do.
    Yes, I have fixed the missing & - too much typing, not enough thinking, as per usual!

    htons() and such functions are definitely the best solution. Many processors have clever instructions that do (or at least help) convert byte-order, so writing your own function to read/write the bytes is not a good idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    We are assuming there are only two kinds of machines... lsb -> msb and the reverse. But what about those whose 16-bit words may be in one order yet the bytes are in another. Or similarly with 32-bit or 64-bit sub chunks may not necessarily be bytes in simple ascending or descending major order.

    I propose that the machine's byte order be expressed as a bit flag value: for example, the trivial cases above would be represented as 0000 or 1111. Such an indicator might be embedded in any files that are expected to be portable across platforms to tell the resident system how the data is to be interpreted.

    The lsb 2^0 (least significant bit) represents whether the bytes are switched
    The next bit 2^1 represents whether 16-bit (short) is switched
    The next bit 2^2 represents whether 32-bit (long) is switched
    The next bit 2^3 represents whether 64-bit(long long) is switched

    The combination of bits thus can portray a machine's byte, short, long, long long ordering in any reasonable permutation. I doubt any machine stores bytes as [0,3,1,2] for example....

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    We are assuming there are only two kinds of machines... lsb -> msb and the reverse. But what about those whose 16-bit words may be in one order yet the bytes are in another. Or similarly with 32-bit or 64-bit sub chunks may not necessarily be bytes in simple ascending or descending major order.
    I find it very unlikely that a machine would use different endianness for different data sizes. And having something else like 2301 ordering seems even less likely.

    If I were you, I'd write most of my code as portably as possible, and have a few wrapper functions to convert between types which just use e.g. htons() etc. Then if you actually encounter such a bizarre machine as you have described, you can modify these wrapper functions as necessary.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by dwks View Post
    I find it very unlikely that a machine would use different endianness for different data sizes. And having something else like 2301 ordering seems even less likely.
    Yes there is.

    From Wiki ( http://en.wikipedia.org/wiki/Endianness )
    Middle-endian

    Still other architectures, generically called middle-endian or mixed-endian, may have a more complicated ordering; PDP-11, for instance, stored some 32-bit words, counting from the most significant, as: 2nd byte first, then 1st, then 4th, and finally 3rd.

    Note that this can be interpreted as storing the most significant "half" (16-bits) followed by the less significant half (as if big-endian) but with each half stored in little-endian format. This ordering is known as PDP-endianness.

    The ARM architecture can also produce this format when writing a 32-bit word to an address 2 bytes from a 32-bit word alignment.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, I stand corrected.

    But I still think that this is a very platform-specific problem which can be solved by writing platform-specific code for your program. I don't think (but watch me be wrong again :P) that your code would necessarily need to deal with all these kinds of endianness in one compiled binary program. I could see you needing to deal with the "local" endianness as well as a "generic", platform-independent endianness for reading binary data files or transferring data over a network, for example; but does your one program need to be able to read PDP-endianness and big-endianness data without being re-compiled?
    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.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I agree with you... the program should not need to be compiled/running any endienness natively. The original problem was to determine the endienness and be able to convert one to another.

    The reason I brought up other oddball endienness is to contend matsp's solution. His is making the assumption there are only two possible orderings.

    I would suggest determining endienness thus:

    Code:
    union {
        long l;
        unsigned char c[4]; } un; 
    
    un.l = 0x03020100l;
    printf("Endienness of this machine (lsb to msb) is: %d %d %d %d\n", un.c[0], un.c[1], un.c[2], un.c[3]);
    Which gives:

    Endienness of this machine (lsb to msb) is: 0 1 2 3

    on my Intel based Dell.

    The second part of the originally stated problem has not as yet been addressed. How to convert one endienness to another.

    I would suggest that once the transformation vector is found, to loop through the bytes of the source 32-bit integer and transposing them using it as guide. This would also require the knowledge of the source endienness as well to find the complete transpose permutation.
    Last edited by nonoob; 03-17-2009 at 04:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a programme with cin.get
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 02:35 PM
  2. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM
  5. Simple C++ GUI programme does'nt run from dos
    By Shadowhunt in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21