iostream.blob.swap4
Byte-swaps the blob contents as an array of 32-bit values
from "iostream" import blob
let cargo = blob(4)
cargo.writen(0x11223344, 'i')
cargo.swap4()
println("cargo[0], cargo[1], cargo[2], cargo[3] =", $"{cargo[0]},{cargo[1]},{cargo[2]},{cargo[3]}")Output:
cargo[0], cargo[1], cargo[2], cargo[3] = 17,34,51,68Return value
None; the blob is modified in place.
Notes
Works in whole 4-byte units starting at byte 0, over the entire buffer; any trailing 1-3 bytes left over from len % 4 are untouched. The cursor (see tell) does not move.
Swapping is its own inverse, so calling swap4 twice restores the original bytes.
Example
from "iostream" import blob
let b = blob(5) // one full 4-byte group plus 1 leftover byte
b.seek(0)
b.writen(0x11223344, 'i')
b[4] = 0x9A
b.swap4()
println("bytes after 1st swap4() =", $"{b[0]},{b[1]},{b[2]},{b[3]}")
println("b[4] =", b[4]) // the leftover byte is never touched
b.swap4() // swapping twice restores the original bytes
println("bytes after 2nd swap4() =", $"{b[0]},{b[1]},{b[2]},{b[3]}")Output:
bytes after 1st swap4() = 17,34,51,68
b[4] = 154
bytes after 2nd swap4() = 68,51,34,17See also
swap2 | Byte-swaps the blob contents as an array of 16-bit values |
swap4 | Byte-swaps a 32-bit value |
| blob | class index |