uint8
func uint8[T: Number](x: T): uint8uint8 converts x to uint8 exactly. It panics if x is out of range, has a fraction, or is NaN or infinite; uint8(Math.trunc(x)) truncates. x may also be a named primitive over a number.
Details
uint8holds whole numbers from 0 to 255. It is the same type asbyte, and types and compile errors show it asbyte.- Arithmetic on two
uint8values gives auint8and panics when the result leaves 0 to 255, including subtraction below zero. For wrapping arithmetic use Bits.wrapAdd and its siblings.
Basic examples
uint8(42) // 42
uint8(0) // 0
uint8(255) // 255A float converts when it is a whole number:
uint8(128.0) // 128
uint8(-0.0) // 0A value outside 0 to 255 panics, and the message names the type byte:
uint8(256) // panic: 256 out of range for byte
uint8(-1) // panic: -1 out of range for byteScope
Any numeric type converts, as long as the value fits:
uint8(int8(100)) // 100
uint8(uint64(7)) // 7
uint8(int16(255)) // 255
uint8(float32(12.0)) // 12
uint8(int8(-1)) // panic: -1 out of range for byteA float with a fraction, NaN, and the infinities panic:
uint8(2.5) // panic: 2.5 has a fraction, so it does not convert to byte
uint8(Math.NaN) // panic: NaN does not convert to byte
uint8(-Math.Infinity) // panic: -Infinity does not convert to byteA named primitive over a number is unwrapped first:
type Percent = int
uint8(Percent(80)) // 80uint8 is a function value, so it can be passed where a function is expected. The result is an Array[byte]:
[0, 128, 255].map(uint8) // [0, 128, 255]In a pipe, the value is the argument:
64.0 | uint8() // 64Indexing Bytes gives a byte, which is a uint8, so it mixes with other uint8 values:
let b = "hi".toBytes()
b[0] + uint8(1) // 105Applications
Turn a color channel from 0.0 to 1.0 into a byte. Round first, since the conversion does not:
func channel(f: float64): uint8 {
uint8(Math.round(Math.clamp(f, 0.0, 1.0) * 255.0))
}
println(channel(0.5))
println(channel(1.0))
println(channel(-0.2))128
255
0Add up the bytes of a Bytes value. Each one is a uint8, so convert it to int before adding, or the sum overflows past 255:
let data = "Ant".toBytes()
var sum = 0
for x in data {
sum += int(x)
}
println(sum)291Split a byte into its high and low four bits with Bits, which keeps the uint8 type:
let b = uint8(0xa7)
let high = Bits.shiftRight(b, 4)
let low = Bits.and(b, uint8(0x0f))
println(high)
println(low)10
7Build Bytes from int values:
let header = [0x89, 0x50, 0x4e, 0x47].map(uint8)
println(Bytes.fromArray(header))Bytes(89 50 4e 47)Properties and relations
Every int from 0 to 255 survives the round trip through uint8:
for n in Iter.range(0, 256) {
assert(int(uint8(n)) == n)
}uint8 and byte are one type, so their values compare equal and is accepts either name:
let n: Number = uint8(9)
assert(n == byte(9))
assert(n is byte)
assert(n is uint8)An Array[uint8] is an Array[byte]:
func total(xs: Array[uint8]): int {
xs.reduce(0, func(acc: int, x: byte) { acc + int(x) })
}
let data: Array[byte] = "ab".toBytes().toArray()
assert(total(data) == 195)Bits.wrapAdd agrees with + whenever + does not overflow:
let a = uint8(200)
let b = uint8(55)
assert(Bits.wrapAdd(a, b) == a + b)Possible issues
An integer literal is an int, never a uint8. The error names the type byte:
let level: uint8 = 5 // error: cannot use int where byte is expectedlet level: uint8 = uint8(5)
level // 5uint8 and int do not mix in arithmetic. Convert one side so that both have the same type:
let level = uint8(3)
let next = level + 1 // error: + needs both operands of one type, got byte and intlet level = uint8(3)
level + uint8(1) // 4
int(level) + 1 // 4Arithmetic overflows quickly in eight bits, and going below zero overflows too:
uint8(200) + uint8(100) // panic: integer overflow
uint8(0) - uint8(1) // panic: integer overflow
Bits.wrapAdd(uint8(200), uint8(100)) // 44
Bits.wrapSub(uint8(0), uint8(1)) // 255The conversion never rounds. A float with a fraction panics, so round or truncate it first:
uint8(0.9 * 255.0) // panic: 229.5 has a fraction, so it does not convert to byte
uint8(Math.round(0.9 * 255.0)) // 230A string does not convert. Parse it with String.toInt, which returns int | null, and convert the result:
let x = uint8("200") // error: byte converts a number or a named primitive type over a number, got stringuint8("200".toInt() ?? 0) // 200See also
byte · int8 · uint16 · int · Bytes.fromArray · Math.round · Math.clamp · Bits.wrapAdd · byte type · Arithmetic
