Blitz3D+ Command Reference

WriteShort stream,short

Parameters

stream - a file stream opened for writing, or a TCP stream

short - value to write; only the low 16 bits (0-65535) are stored

Description

Writes a 16-bit value (0-65535) to a file or stream.

A short occupies 2 bytes, stored least significant byte first. Only the low 16 bits of the value are kept, so 0 to 65535 store faithfully and ReadShort returns them unsigned - a -1 written here reads back as 65535.

Shorts halve the size of an int, which adds up nicely in big files of tile indices or network messages where every byte counts.

See also: ReadShort, WriteByte, WriteInt.

Example

; WriteShort Example
; ------------------

; WriteShort stores a 2-byte value (0 to 65535); only the low
; 16 bits of the number are kept
file=WriteFile("demo_shorts.dat")
WriteShort file,10
WriteShort file,65535
WriteShort file,65536
WriteShort file,-1
CloseFile file

Print "Wrote 4 shorts - demo_shorts.dat is "+FileSize("demo_shorts.dat")+" bytes (4 x 2)"

; Read them back to see the wrap-around
file=ReadFile("demo_shorts.dat")
Print "  10    -> "+ReadShort(file)
Print "  65535 -> "+ReadShort(file)+"  (largest value a short can hold)"
Print "  65536 -> "+ReadShort(file)+"  (wrapped around to 0)"
Print "  -1    -> "+ReadShort(file)+"  (wrapped around to 65535)"
CloseFile file

; Clean up the demo file
DeleteFile "demo_shorts.dat"
Print "Deleted demo_shorts.dat"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index