Endianess on Intel Macs

Discussion in 'Mac Programming' started by Eraserhead, Oct 16, 2007.

  1. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #1
    I have been given a programming assignment which has a file in Unix "Big Endian" format, as I have an Intel Mac, I'm assuming that I'll need to convert the file to Little Endian format to read it in?

    If I compile the code for PPC only and then use Rosetta will it read files in as Big Endian, thus saving me the trouble of making a converter. (I'll be finally running it on a Sun UNIX system from 2000).
     
  2. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #2
    Yes to both. (To clarify, yes you have to make the endian adjustment if you compile as a universal or Intel-only binary, and yes you won't have to do this if you compile for PPC only and run in Rosetta.)

    Depending on the complexity of the file you're reading, it may not be all that complex to make your code architecture neutral. Take a look at NSSwapBigIntToHost() and friends.
     
  3. Eraserhead thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    Thanks :). Its not written in Obj-C and has to run on non-Mac platforms so I don't think I can use NSSwapBigIntToHost().
     
  4. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #4
    Er, right! I should probably have worked that out from your first post... :)
     
  5. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #5
    If I compile the code for PPC only and then use Rosetta will it read files in as Big Endian, thus saving me the trouble of making a converter..[/QUOTE]

    Do it "right". There are macros that make doing it right easy. For starts look up the "htons" man page. The man pages talk about "network byte order". Wich means the convention used by Solaris SPARC. or Big Endian. The macros convert from network order to whatever your system does. On the SPARC the macro does nothing on the Intel i386 they swap the bytes. Use the correct macros and your code will "just work".

    htons and family are available on ALL unix-like systems and is completely portable.

    Remember Solaris now can go both ways depending if it is running on SPARC or Intel/AMD.

    (No Mac here but My Linux system has a "byteorder" man page that includes about 6 macros for this purpose.)
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    The file you have is in bigendian format. "BigEndian" is also called "network byte order". There are standard Posix macros which read data in network byte order and convert it to the correct value for your application: ntohl ("Network to Host Long") converts a 32 bit value and ntohs ("Network to Host Short") converts a 16 bit value.

    Using these functions means you can read any bigendian file, no matter what your own system is using, without any code changes.

    Your idea of handling the problem by writing non-portable PPC code would mean that you didn't understand the assignment, therefore immediate and complete failure of the assignment.
     
  7. Eraserhead thread starter macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    Yeah it seems to be an Ultra-SPARC chip (from gcc -v), so that'll make it Big Endian.

    In general I agree. However in this specific case we aren't handling the file input itself and we are just supposed to be running it on a remote Unix system. I'm just compiling it on my Mac for testing purposes as its much easier than copying it over to the remote system.
     

Share This Page