Bytes.toArray
func Bytes.toArray(b: Bytes): Array[byte]Bytes.toArray returns the bytes of b as an array.
Details
- The array is new. Changing it leaves
bas it was, and changingbleaves the array.
Basic examples
Turn bytes into an array of numbers:
Bytes.toArray(Bytes.fromArray([0x89, 0x50, 0x4e, 0x47])) // [137, 80, 78, 71]Read the UTF-8 bytes of a string as numbers:
Bytes.toArray(String.toBytes("hi")) // [104, 105]Scope
Empty bytes give an empty array:
String.toBytes("").toArray() // []A character outside ASCII takes several bytes, each its own element:
String.toBytes("é").toArray() // [195, 169]
String.toBytes("🐜").toArray() // [240, 159, 144, 156]Call it through . or in a pipe:
let b = String.toBytes("Ant")
b.toArray() // [65, 110, 116]
b | Bytes.toArray() // [65, 110, 116]Applications
Change one byte and build new bytes from the array. Here the version byte of a small header goes from 1 to 2:
let header = Bytes.fromArray([0x41, 0x4e, 0x54, 0x01])
let fields = header.toArray()
fields[3] = byte(2)
Bytes.fromArray(fields) // Bytes(41 4e 54 02)Flip the case of ASCII letters with Bits.xor, then decode the result:
let flipped = String.toBytes("Ant").toArray().map({ Bits.xor(x, byte(0x20)) })
String.fromBytes(Bytes.fromArray(flipped)) // "aNT"Add up the bytes as ints for a simple checksum, kept in one byte with %:
func checksum(b: Bytes): int {
(b.toArray().map({ int(x) }).sum() ?? 0) % 256
}
checksum(String.toBytes("hello")) // 20Count the bytes that are not ASCII:
String.toBytes("naïve café").toArray().countWhere({ x > byte(127) }) // 4Properties and relations
Bytes.fromArray turns the array back into the same bytes:
let b = String.toBytes("ant")
assert(Bytes.fromArray(Bytes.toArray(b)) == b)The array has one element per byte, in order, each equal to indexing b:
let b = String.toBytes("añt")
let xs = b.toArray()
assert(len(xs) == len(b))
assert(Array.range(0, len(b)).every({ xs[x] == b[x] }))Changing the array leaves the bytes as they were:
let b = String.toBytes("ant")
let xs = b.toArray()
xs[0] = byte(0)
assert(b == String.toBytes("ant"))Possible issues
Indexing, len, and for work on Bytes directly; convert with Bytes.toArray only to use an Array function or to change bytes:
let b = String.toBytes("hi")
b[0] // 104
len(b) // 2let b = String.toBytes("hi")
for x in b {
println(x)
}104
105An element is a byte, so adding an int literal to it is a compile error. Convert one side:
let xs = String.toBytes("A").toArray()
let next = xs[0] + 1 // error: + needs both operands of one type, got byte and intlet xs = String.toBytes("A").toArray()
xs[0] + byte(1) // 66
int(xs[0]) + 1 // 66Arithmetic on bytes panics outside 0 to 255. Compute in int instead:
let xs = Bytes.fromArray([200, 100]).toArray()
xs.sum() // panic: Array.sum: integer overflow
xs.map({ int(x) }).sum() ?? 0 // 300
xs[1] - xs[0] // panic: integer overflow
int(xs[1]) - int(xs[0]) // -100An array of bytes and Bytes are different types, so == between them is a compile error. Convert one side first:
let b = String.toBytes("A")
let same = b.toArray() == b
// error: Array[byte] and Bytes cannot be compared with ==: neither is assignable to the otherThe array renders its bytes in decimal, while Bytes renders them in hexadecimal:
let b = Bytes.fromArray([0x0a, 0xff])
b // Bytes(0a ff)
b.toArray() // [10, 255]See also
Bytes.fromArray, Bytes.length, Bytes.slice, String.toBytes, String.fromBytes, Encoding.toHex, Bits.xor
