Blitz3D+ Command Reference

WriteByte stream,byte

Parameters

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

byte - value to write; only the low 8 bits (0-255) are stored

Description

Writes one byte (0-255) to a file or stream.

A byte is 8 bits, so only values 0 to 255 store faithfully - anything else keeps just its low 8 bits (writing 256 stores 0, writing -1 stores 255). That is how integers work, not a bug. Since characters are single bytes, WriteByte can build a text file one character at a time (Asc gives you the byte for a character).

When you have a lot of bytes to move, put them in a bank and use WriteBytes instead - one big write beats thousands of little ones.

See also: ReadByte, WriteShort, WriteInt, WriteBytes.

Example

; WriteByte Example
; -----------------

; WriteByte stores a single byte (0 to 255); only the low
; 8 bits of the number are kept
file=WriteFile("demo_bytes.dat")
WriteByte file,65
WriteByte file,255
WriteByte file,256
WriteByte file,-1
CloseFile file

Print "Wrote 4 bytes - demo_bytes.dat is "+FileSize("demo_bytes.dat")+" bytes"

; Read them back to see the wrap-around
file=ReadFile("demo_bytes.dat")
a=ReadByte(file)
Print "  65  -> "+a+"  (the ASCII code for "+Chr$(a)+")"
Print "  255 -> "+ReadByte(file)+"  (largest value a byte can hold)"
Print "  256 -> "+ReadByte(file)+"  (wrapped around to 0)"
Print "  -1  -> "+ReadByte(file)+"  (wrapped around to 255)"
CloseFile file

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

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

End

Index