PC vs Mac - Reading and Writing Integers

BlitzMax Forums/BlitzMax Programming/PC vs Mac - Reading and Writing Integers

AvestheFox(Posted 2011) [#1]
On a PC when I write a variable to a file as an integer it retains its value when the file is loaded up again. But when I move the code to my Mac PowerPC its an entirely different value.

Is this a bug?


Brucey(Posted 2011) [#2]
PPC uses Big-Endian, X86 uses Little-Endian - see Endianness for more details :-)

Basically, the byte-order is reversed.

There are TStreams which support different endianness, so you can use those to ensure that the data is loaded/saved in the same way on all platforms.


AvestheFox(Posted 2011) [#3]
right, I'm looking at the endian stream functions now...

could someone please show me how I could use these? the documentation lacks samples :P


Brucey(Posted 2011) [#4]
Create your stream as usual, but then push it into either LittleEndianStream() or BigEndianStream().
Those functions return a new TStream which wraps your original stream, and applies endian conversion if necessary.

So, possibly something like :

Local myfile:TStream = LittleEndianStream(WriteStream("myfile.txt"))

myfile.WriteInt(12345)

myfile.Close()




Czar Flavius(Posted 2011) [#5]
Pick either little or big endian stream (arbitrary choice), and then always use that type for every file operation. Problem solved ;D


AvestheFox(Posted 2011) [#6]
alrighty, thanks! I'll give it a go :)