BlitzMax/Modules/Streams/Endian streams

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Endian streams allow you to swap the byte order of stream data.

This can be useful for reading or writing binary streams which are in a fixed endian format on multiple platforms.

Endian format refers to the order multi-byte values are stored. For example, an integer is four bytes long, but in what order should each byte be stored? Low byte first or high byte first?

This choice is generally dictated by the CPU. For example, Intel CPU's such as the pentium store multi-byte values 'low byte first' which is known as little endian. On the other hand, PowerPC CPU's store multi-byte values 'high byte first' which is known as big endian.

Most of the time, you don't have to worry about endian issues. As long as the same endian format is being used to read and write data, there is no problem. However, endian issues do need to be taken into account when reading or writing binary files. For example, writing a binary file on a PC (which is little endian) and reading it back on a Mac (which is big endian) may produce odd results if the file contains multibyte values.

Endian streams help solve this problem by modifying the behaviour of stream commands that read or write multi-byte values. These commands are: ReadShort, ReadInt, ReadLong, ReadFloat, ReadDouble, WriteShort, WriteInt, WriteLong, WriteFloat and WriteDouble.

Functions[edit | edit source]

BigEndianStream[edit | edit source]

Function BigEndianStream:TStream( stream:TStream )

Description: BigEndianStream

Returns: A big endian stream

LittleEndianStream[edit | edit source]

Function LittleEndianStream:TStream( stream:TStream )

Description: LittleEndianStream

Returns: A little endian stream