Working with binary data
Factor handbook > Input and output

Prev:Byte-array streams
Next:Formatted output

Stream words on binary streams only read and write byte arrays. Packed binary integers can be read and written by converting to and from sequences of bytes. Floating point numbers can be read and written by converting them into a their bitwise integer representation (Floats).

There are two ways to order the bytes making up an integer; little endian byte order outputs the least significant byte first, and the most significant byte last, whereas big endian is the other way around.

Consider the hexadecimal integer 0xcafebabe. Little endian byte order yields the following sequence of bytes:

Compare this with big endian byte order:

Two words convert a sequence of bytes into an integer:
be> ( seq -- x )

le> ( seq -- x )

Two words convert an integer into a sequence of bytes:
>be ( x n -- byte-array )

>le ( x n -- byte-array )

Words for taking larger integers apart into smaller integers:
d>w/w ( d -- w1 w2 )

w>h/h ( w -- h1 h2 )

h>b/b ( h -- b1 b2 )