byte
func byte[T: Number](x: T): bytebyte converts x to byte, which is uint8, exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; byte(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
byteanduint8are one type under two names, and Ant shows it asbyte.- An integer literal is always an
int, so abytecomes from a conversion such asbyte(65)or from readingBytes. - Arithmetic on two
bytevalues stays abyteand panics when the result leaves 0 to 255.
Basic examples
byte(65) // 65
byte(0) // 0
byte(255) // 255A float converts when it has no fraction:
byte(65.0) // 65A value outside 0 to 255 panics:
byte(256) // panic: 256 out of range for byte
byte(-1) // panic: -1 out of range for byteScope
Every numeric kind converts, as long as the value fits:
byte(int8(100)) // 100
byte(uint64(7)) // 7
byte(float32(12.0)) // 12
byte(uint8(200)) // 200A float with a fraction panics, and so do NaN and the infinities:
byte(65.5) // panic: 65.5 has a fraction, so it does not convert to byte
byte(Math.NaN) // panic: NaN does not convert to byte
byte(Math.Infinity) // panic: Infinity does not convert to byteA named primitive over a number is unwrapped first:
type Level = int
byte(Level(3)) // 3byte is a function value, so it converts every element of an array:
[72, 105].map(byte) // [72, 105]
[72.0, 105.0].map(byte) // [72, 105]In a generic function, a type parameter bounded by Number converts too:
func toByte[T: Number](x: T): byte {
byte(x)
}
toByte(int16(7)) // 7
toByte(9.0) // 9Indexing Bytes gives a byte, and int converts it back:
let b = "Hi".toBytes()
b[0] // 72
int(b[0]) + 1 // 73Applications
Build Bytes from computed values:
[0, 1, 2, 3] | Array.map({ byte(x * 85) }) | Bytes.fromArray() // Bytes(00 55 aa ff)Sum the bytes of a message into a one-byte checksum. The sum is kept in an int, which has room for it, and only the remainder becomes a byte:
func checksum(data: Bytes): byte {
var sum = 0
for b in data {
sum += int(b)
}
byte(sum % 256)
}
println(checksum("hello".toBytes()))20Mask each byte with a one-byte key using Bits.xor, which keeps the byte kind:
let key = byte(0x2a)
let secret = "ant".toBytes().toArray().map({ Bits.xor(x, key) })
println(Bytes.fromArray(secret))
let plain = secret
| Array.map({ Bits.xor(x, key) })
| Bytes.fromArray()
| String.fromBytes()
println(plain)Bytes(4b 44 5e)
antProperties and relations
Every int from 0 to 255 survives the round trip through byte:
for i in Iter.range(0, 256) {
assert(int(byte(i)) == i)
}byte and uint8 give equal values of one type:
assert(byte(42) == uint8(42))Possible issues
An integer literal is an int, and Ant converts nothing implicitly:
let flag: byte = 1 // error: cannot use int where byte is expectedConvert the literal:
let flag = byte(1)
flag // 1A byte and an int do not mix in arithmetic or comparisons:
let b = "A".toBytes()
println(b[0] + 1) // error: + needs both operands of one type, got byte and intArithmetic on byte values panics instead of wrapping around:
byte(250) + byte(10) // panic: integer overflow
byte(0) - byte(1) // panic: integer overflowFor arithmetic that wraps around, use Bits.wrapAdd, Bits.wrapSub, or Bits.wrapMul:
Bits.wrapAdd(byte(250), byte(10)) // 4
Bits.wrapSub(byte(0), byte(1)) // 255Or convert to int first, and take the remainder before converting back:
byte((int(byte(250)) + 10) % 256) // 4A float with a fraction does not round. Truncate it first:
byte(Math.trunc(65.7)) // 65byte takes a number, not a character. Get the bytes of a string with String.toBytes:
println(byte("A"))
// error: byte converts a number or a named primitive type over a number, got string"A".toBytes()[0] // 65See also
uint8 · int · Bytes · Bytes.fromArray · Bytes.toArray · String.toBytes · Math.trunc · Bits.wrapAdd · byte type
