.\" Made by website/plain/build.mjs from website/words/byte.md; change that file. .TH SG-BYTE 7 2026-11-28 "SG" "SG reference" .SH NAME byte \- an integer of 8 bits, 0 to 255: one byte of a text, a file or a buffer .SH SYNOPSIS .TP \fBbyte b = 200, 0xff, 0b1010_0000\fR a number from 0 to 255, written as any integer is .TP \fBbyte b = 'A'\fR the code of a character of ASCII, where a byte goes .TP \fBtext[i], text.asBytes()\fR one byte of a text, all of them as a read byte[] .TP \fBList, read byte[], byte4\fR bytes a list owns, a window onto bytes, an array of four .TP \fBint x = b\fR a byte becomes a wider integer on its own .TP \fB(byte)n, n.tryToByte(), n.wrappingToByte()\fR a wider number made a byte: a panic, null, or the low 8 bits when it does not fit .TP \fB{b:x2}, {b:X}, {b:b8}\fR the byte in hex or in binary, in $"..." .TP \fBstring.fromUtf8(bytes)\fR bytes as text, once checked .SH DESCRIPTION A \fBbyte\fR is a whole number from 0 to 255 in 8 bits, never negative. A number written in the program has no mark of its type: \fB200\fR, \fB0xff\fR and \fB\&'A'\fR are each a \fBbyte\fR where a \fBbyte\fR goes, and a number that does not fit is refused when the program is compiled. A character constant is a \fBbyte\fR only when it takes one byte in UTF-8. .PP Arithmetic happens in the type of its operands, so \fBbyte + byte\fR is a \fBbyte\fR, and a sum past 255 is an overflow: a panic in the interpreter, in tests and in a debug build, and a wrap in a release build. \fBwrappingAdd\fR, \fBtryAdd\fR and \fBsaturatingAdd\fR say which one is meant in every build. A \fBbyte\fR becomes an \fBint\fR, a \fBlong\fR, an \fBnint\fR or another wider type on its own; the way back is a cast, \fB(byte)n\fR, which panics when the value does not fit, and keeps the low 8 bits in a release build. .PP Bytes in a row are raw data: the bytes of a file, of a connection, of a text. A \fBList\fR owns them and a \fBread byte[]\fR looks at them where they lie; \fBbyte[]\fR alone is not a type. \fBtext[i]\fR reads one byte of a text, and \fBtext.asBytes()\fR lends them all, with no copy. A \fBbyte\fR is not a \fBchar\fR: the two are never compared, and \fB(char)b\fR and \fB(byte)c\fR go between them. .PP The methods are in Numbers; the tests of ASCII (\fBisAsciiDigit\fR, \fBtoAsciiUpper\fR ...) are in Text. .SH EXAMPLES .SS "A number from 0 to 255" .RS 4 .nf void main() { byte level = 200 byte step = 0x20 byte mask = 0b1111_0000 print($"{level} {step} {mask}") print(level + step) int wide = level print(wide + 100) print($"{level:x2} {level:X} {mask:b8}") } .fi .RE .PP .RS 4 .nf 200 32 240 232 300 c8 C8 11110000 .fi .RE .PP \fBlevel + step\fR is a \fBbyte\fR, and 232 fits. \fBwide + 100\fR is an \fBint\fR: \fBlevel\fR widens on its way into \fBwide\fR\&. .SS "The bytes of a text" .RS 4 .nf void main() { var line = "name: naïve" print(line[4]) print(line[4] == ':') var bytes = line.asBytes() print(bytes[6..]) nint high = 0 for b in bytes { if b >= 0x80 { high += 1 } } print($"{bytes.length()} bytes, {high} outside ASCII") } .fi .RE .PP .RS 4 .nf 58 true [110, 97, 195, 175, 118, 101] 12 bytes, 2 outside ASCII .fi .RE .PP \fBline[4]\fR is the byte of \fB:\fR, and prints as a number. \fB\&':'\fR is a \fBbyte\fR beside it. \fBï\fR is two bytes in UTF-8, 195 and 175, both above 127. .SS "Digits out of bytes" .RS 4 .nf long? number(read byte[] digits) { if digits.length() == 0 { return null } long n = 0 for b in digits { if !b.isAsciiDigit() { return null } n = n * 10 + (b - '0') } return n } void main() { var header = "Content-Length: 1234".asBytes() print(number(header[16..]) ?? -1) print(number(header[..7]) ?? -1) } .fi .RE .PP .RS 4 .nf 1234 -1 .fi .RE .PP \fBb \- '0'\fR is a \fBbyte\fR from 0 to 9, which widens to a \fBlong\fR in the sum. \fBheader[16..]\fR looks at the last four bytes, with no copy. .SS "A checksum" .RS 4 .nf byte checksum(read string sentence) { byte sum = 0 for b in sentence.asBytes() { sum ^= b } return sum } bool valid(read string line) { if line.length() < 4 || line[0] != '$' || line[^3] != '*' { return false } var given = int.parseHex(line[^2..]) ?? -1 return given == checksum(line[1..^3]) } void main() { var body = "GPGLL,4916.45,N,12311.12,W,225444,A," print($"${body}*{checksum(read body):X2}") print(valid("$GPGLL,4916.45,N,12311.12,W,225444,A,*1D")) print(valid("$GPGLL,4916.45,N,12311.12,W,225445,A,*1D")) } .fi .RE .PP .RS 4 .nf $GPGLL,4916.45,N,12311.12,W,225444,A,*1D true false .fi .RE .PP A sentence of NMEA, the text a GPS receiver sends, ends with the bytes between \fB$\fR and \fB*\fR joined by \fB^\fR, in two hex digits. \fB^\fR on two bytes is a \fBbyte\fR, so it never overflows. The \fBbyte\fR widens to an \fBint\fR to be compared with \fBgiven\fR\&. .SS "Wrapping on purpose" .RS 4 .nf uint fnv1a(read byte[] data) { uint hash = 2166136261 for b in data { hash ^= b hash = hash.wrappingMultiply(16777619) } return hash } void main() { print($"{fnv1a("".asBytes()):x8}") print($"{fnv1a("a".asBytes()):x8}") byte b = 250 print(b.wrappingAdd(10)) print(b.tryAdd(10) ?? 0) print((300).wrappingToByte()) } .fi .RE .PP .RS 4 .nf 811c9dc5 e40c292c 4 0 44 .fi .RE .PP A hash means to wrap, so it says so with \fBwrappingMultiply\fR, and runs the same in every build. Each \fBbyte\fR widens to a \fBuint\fR in \fBhash ^= b\fR\&. \fBwrappingToByte\fR keeps the low 8 bits of 300: 44. .SS "The bytes of a file" .RS 4 .nf Result main() { var folder = try Directory.createTemp() var path = Path.join(read folder, "notes.txt") try File.writeText(read path, "one\er\entwo\ennaïve\en") var bytes = try File.readBytes(read path) nint lines = 0 nint returns = 0 for b in read bytes { match b { '\en' => lines += 1 '\er' => returns += 1 _ => {} } } print($"{bytes.length()} bytes, {lines} lines, {returns} with \e\er") print(Hex.encode(bytes[..5])) bytes[0] = bytes[0].toAsciiUpper() print(string.fromUtf8(bytes[..3]) ?? "not UTF-8") print(string.fromUtf8(bytes[..12]) ?? "not UTF-8") try Directory.deleteAll(read folder) return Ok() } .fi .RE .PP .RS 4 .nf 16 bytes, 3 lines, 1 with \er 6f6e650d0a One not UTF-8 .fi .RE .PP \fBFile.readBytes\fR gives a \fBList\fR, checked for nothing. A \fBmatch\fR on a \fBbyte\fR takes characters of ASCII as patterns. The list owns its bytes, so one can be changed; a text cannot. The first 12 bytes end in the middle of \fBï\fR, so they are not text. .SH "WHAT STOPS THE PROGRAM" .SS "A sum past 255" .RS 4 .nf void main() { var data = List { 250, 3, 4 } byte sum = 0 for b in data { sum += b } print(sum) } .fi .RE .PP .RS 4 .nf panic: overflow: 253 + 4 does not fit in byte --> example.sg:5:9 5 | sum += b | ^^^^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around .fi .RE .PP A sum of many bytes is kept in a wider type: \fBint sum = 0\fR takes each \fBbyte\fR as it is. .SS "A cast that does not fit" .RS 4 .nf void main() { var sizes = List { 200, 300 } for n in sizes { byte b = (byte)n print(b) } } .fi .RE .PP .RS 4 .nf 200 panic: value 300 does not fit in byte --> example.sg:4:18 4 | byte b = (byte)n | ^^^^^^^ hint: `tryToByte()` gives `null` when the value does not fit, and `wrappingToByte()` cuts it to size .fi .RE .PP A release build cuts the value to its low 8 bits instead, as for an overflow. .SH "WHAT THE COMPILER REFUSES" .SS "A number that does not fit" .RS 4 .nf void main() { byte b = 256 print(b) } .fi .RE .PP .RS 4 .nf error: `256` does not fit in byte --> example.sg:2:14 2 | byte b = 256 | ^^^ .fi .RE .PP \fBbyte b = \-1\fR is refused the same way, with \fBa `byte` is never negative\fR, and so is \fB(byte)300\fR\&. .SS "A minus" .RS 4 .nf void main() { byte b = 7 print(-b) } .fi .RE .PP .RS 4 .nf error: operator `-` is not defined for `byte`: a `byte` is never negative --> example.sg:3:11 3 | print(-b) | ^^ hint: convert first: `-(int)b` .fi .RE .SS "An int where a byte goes" .RS 4 .nf void main() { int count = 70 byte b = count print(b) } .fi .RE .PP .RS 4 .nf error: expected `byte`, found `int` --> example.sg:3:14 3 | byte b = count | ^^^^^ hint: convert with `(byte)count`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A sum that is meant to be an int" .RS 4 .nf void main() { byte b = 200 int total = b + 1000 print(total) } .fi .RE .PP .RS 4 .nf error: `1000` does not fit in byte --> example.sg:3:21 3 | int total = b + 1000 | ^^^^ hint: compute in `int`: `(int)b + 1000` .fi .RE .PP The sum is a \fBbyte\fR, since \fBb\fR is one, and is widened after it is found. \fB(int)b + 1000\fR adds in an \fBint\fR\&. .SS "A byte beside a char" .RS 4 .nf void main() { var text = "abc" var first = 'a' print(text[0] == first) } .fi .RE .PP .RS 4 .nf error: operator `==` is not defined for `byte` and `char` --> example.sg:4:11 4 | print(text[0] == first) | ^^^^^^^^^^^^^^^^ hint: a `char` is not a number; compare its code: `text[0] == (int)first` .fi .RE .PP \fBfirst\fR is a \fBchar\fR, since nothing said otherwise. \fBtext[0] == 'a'\fR is fine: the constant becomes a \fBbyte\fR there. .SS "A character of two bytes" .RS 4 .nf void main() { var text = "café" print(text[3] == 'é') } .fi .RE .PP .RS 4 .nf error: `é` does not fit in one byte: it takes 2 bytes in UTF-8 --> example.sg:3:22 3 | print(text[3] == 'é') | ^^^ hint: look for the character where the byte is: `text[3..].startsWith('é')` .fi .RE .PP \fBtext[3..].startsWith('é')\fR asks the question of the text, by characters. .SS "byte[] alone" .RS 4 .nf void main() { byte[] data = "SG".asBytes() print(data) } .fi .RE .PP .RS 4 .nf error: `byte[]` is not a type on its own: a slice only looks at items that something else owns --> example.sg:2:5 2 | byte[] data = "SG".asBytes() | ^^^^^^ hint: `List` to own the items, `read byte[]` for a window onto them .fi .RE .SH "SEE ALSO" .BR sg-char (7), .BR sg-string (7), .BR sg-list (7), .BR sg-slice (7), .BR sg-int (7) .PP https://sg\-lang.org/reference/byte.html