Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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).
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
Yes to both.

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.

Thanks :). Its not written in Obj-C and has to run on non-Mac platforms so I don't think I can use NSSwapBigIntToHost().
 

ChrisA

macrumors G5
Jan 5, 2006
12,569
1,680
Redondo Beach, California
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.)
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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).

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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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.

Yeah it seems to be an Ultra-SPARC chip (from gcc -v), so that'll make it Big Endian.

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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.