Quirrel4.41.0

iostream.blob.swap2

Method of blob, from module "iostream"
instance.swap2()

Byte-swaps the blob contents as an array of 16-bit values

examples/iostream/blob/swap2-basic.nut
from "iostream" import blob

let cargo = blob(2)
cargo.writen(0x1234, 's')
cargo.swap2()
println("cargo[0], cargo[1] =", $"{cargo[0]},{cargo[1]}")
Output:
cargo[0], cargo[1] = 18,52

Return value

None; the blob is modified in place.

Notes

Works in whole 2-byte units starting at byte 0, over the entire buffer; if len is odd, the last byte is left untouched. The cursor (see tell) does not move.

Swapping is its own inverse, so calling swap2 twice restores the original bytes.

Example

examples/iostream/blob/swap2.nut
from "iostream" import blob

let b = blob(5)   // odd length: 2 full pairs plus 1 leftover byte
b.seek(0)
b.writen(0x1234, 's')
b.writen(0x5678, 's')
b[4] = 0x9A

b.swap2()
println("bytes after 1st swap2() =", $"{b[0]},{b[1]},{b[2]},{b[3]}")
println("b[4] =", b[4])       // the leftover byte is never touched

b.swap2()           // swapping twice restores the original bytes
println("bytes after 2nd swap2() =", $"{b[0]},{b[1]},{b[2]},{b[3]}")
Output:
bytes after 1st swap2() = 18,52,86,120
b[4] = 154
bytes after 2nd swap2() = 52,18,120,86

See also

swap4Byte-swaps the blob contents as an array of 32-bit values
swap2Byte-swaps a 16-bit value
blobclass index