byte
byte is an unsigned 8-bit integer, from 0 to 255. It is another name for uint8. A byte is the element of binary data: indexing Bytes or looping over it gives a byte.
Details
byteanduint8are one type under two names. Ant always shows it asbyte.- An integer literal is always an
int. Abytecomes from a conversion,byte(x), which panics whenxhas a fraction or is outside 0 to 255. - Arithmetic needs two operands of one type, so a
bytenever mixes with anintwithout a conversion. A result outside 0 to 255 panics, subtraction below zero included.Bits.wrapAdd,Bits.wrapSub, andBits.wrapMulwrap instead. - Bitwise operations are functions in the
Bitsnamespace, not operators.
Basic examples
Convert an int to a byte, and back:
let b = byte(65)
b // 65
int(b) // 65Read the bytes of binary data:
let data = String.toBytes("Hi")
data[0] // 72
data.toArray() // [72, 105]Scope
uint8 is the same type
uint8 names the same type, and it is shown as byte:
type(uint8) // byte
byte(255) == uint8(255) // true
type(Array[uint8]) // Array[byte]Conversions
byte converts any numeric value that fits exactly, a float without a fraction included:
byte(0) // 0
byte(255) // 255
byte(3.0) // 3
byte(int8(7)) // 7As a function value, byte converts every element of an array:
Array.map([72, 105], byte) // [72, 105]Arithmetic and comparison
Two byte values add, subtract, and compare like any integers, and the result is a byte:
let a = byte(200)
let b = byte(50)
a + b // 250
a - b // 150
a / b // 4
a > b // trueConvert to int to compute past 255:
let a = byte(200)
let b = byte(100)
int(a) + int(b) // 300Bits
Bits functions on a byte give a byte, except the counts: Bits.popCount, Bits.leadingZeros, and Bits.trailingZeros return an int, and a shift takes its count as an int:
let b = byte(0xa5)
Bits.shiftRight(b, 4) // 10
Bits.and(b, byte(0x0f)) // 5
Bits.xor(b, byte(0xff)) // 90
Bits.not(b) // 90
Bits.popCount(b) // 4
Bits.wrapAdd(byte(255), byte(1)) // 0Bytes and loops
for over Bytes binds each byte as a byte:
var sum = 0
for b in String.toBytes("ant") {
sum += int(b)
}
println(sum)323An Array[byte] and Bytes convert to each other:
let raw = [byte(0x89), byte(0x50)]
Bytes.fromArray(raw) // Bytes(89 50)
Bytes.fromArray(raw).toArray() == raw // trueRendering
A byte renders as a decimal number. String.fromInt writes it in another base:
let b = byte(0xa5)
"${b}" // "165"
String.fromInt(b, base: 16) // "a5"
String.fromInt(b, base: 2) // "10100101"
String.fromInt(byte(7), base: 16).padStart(2, "0") // "07"Applications
A one-byte checksum
Bits.wrapAdd keeps the sum in a byte, wrapping past 255:
func checksum(data: Bytes): byte {
var sum = byte(0)
for b in data {
sum = Bits.wrapAdd(sum, b)
}
sum
}
println(checksum(String.toBytes("ant")))67A hex dump
Each byte as two hexadecimal digits:
let hex = String.toBytes("Ant!").toArray()
| Array.map({ String.fromInt(x, base: 16).padStart(2, "0") })
| String.join(" ")
println(hex)41 6e 74 21High and low nibbles
func nibbles(b: byte): Array[byte] {
[Bits.shiftRight(b, 4), Bits.and(b, byte(0x0f))]
}
println(nibbles(byte(0x3c)))[3, 12]XOR with a key
Compute bytes one at a time into new Bytes:
func xorWith(data: Bytes, key: byte): Bytes {
let out = Bytes.new(len(data))
for i in Array.range(0, len(data)) {
out[i] = Bits.xor(data[i], key)
}
out
}
let secret = xorWith(String.toBytes("ant"), byte(0x2a))
println(secret)
println(String.fromBytes(xorWith(secret, byte(0x2a))))Bytes(4b 44 5e)
antProperties and relations
Every int from 0 to 255 converts to a byte and back unchanged:
for n in Array.range(0, 256) {
assert(int(byte(n)) == n)
}Wrapping arithmetic is arithmetic modulo 256:
let a = byte(200)
let b = byte(100)
assert(int(Bits.wrapAdd(a, b)) == (int(a) + int(b)) % 256)
assert(int(Bits.wrapSub(b, a)) == (int(b) - int(a) + 256) % 256)Possible issues
An integer literal is an int
A literal does not become a byte from the type around it. Convert it:
let b: byte = 65 // error: cannot use int where byte is expectedlet b: byte = byte(65)
let xs: Array[byte] = [1, 2].map(byte)Mixing byte and int
let b = byte(1)
let c = b + 1 // error: + needs both operands of one type, got byte and intConvert one side: b + byte(1) stays a byte, int(b) + 1 is an int.
Overflow and underflow panic
byte(200) + byte(100) // panic: integer overflow
byte(0) - byte(1) // panic: integer overflowA shift of 8 or more panics
A shift count must be below the operand's width, which is 8 for a byte:
Bits.shiftLeft(byte(1), 8) // panic: Bits.shiftLeft needs a count from 0 up to but excluding 8, got 8To combine two bytes into a wider integer, convert first:
let hi = byte(0x12)
let lo = byte(0x34)
Bits.or(Bits.shiftLeft(int(hi), 8), int(lo)) // 4660Conversions out of range panic
byte(256) // panic: 256 out of range for byte
byte(-1) // panic: -1 out of range for byte
byte(1.5) // panic: 1.5 has a fraction, so it does not convert to byteA string escape is not a byte
\u{...} writes a code point, which UTF-8 may store as more than one byte:
String.toBytes("\u{89}") // Bytes(c2 89)
Bytes.fromArray([0x89]) // Bytes(89)A byte of Bytes takes a byte
data[i] = x writes a byte, so an int is converted first:
let data = String.toBytes("Hi")
data[0] = 104
// error: cannot use int where byte is expectedSee also
int, Bytes, Bytes.fromArray, Bytes.toArray, Bits, Bits.wrapAdd, String.toBytes, String.fromInt, Number literals, Arithmetic operators
