iostream.blob.swap2
Byte-swaps the blob contents as an array of 16-bit values
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,52Return 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
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,86See also
swap4 | Byte-swaps the blob contents as an array of 32-bit values |
swap2 | Byte-swaps a 16-bit value |
| blob | class index |