iostream.blob.as_string
Returns the blob contents as a string
from "iostream" import blob
let cargo = blob(0)
cargo.writestring("ammo")
println("cargo.as_string() =", cargo.as_string())Output:
cargo.as_string() = ammoReturn value
A string of len bytes. It can contain embedded zero bytes, since the length is passed along explicitly rather than found by scanning for a terminator.
Notes
Unlike readblob, as_string always starts at byte 0 and ignores the cursor: it does not consult or move tell. Read the blob through readblob or readn instead when the cursor position should matter.
Example
from "iostream" import blob
let b = blob(3)
b[0] = 65 // 'A'
b[1] = 0 // embedded zero byte, kept as-is
b[2] = 66 // 'B'
b.seek(2) // move the cursor away from the start
let s = b.as_string()
println("s.len() =", s.len()) // as_string ignores the cursor: all 3 bytes
println("s[0], s[1], s[2] =", $"{s[0]},{s[1]},{s[2]}")Output:
s.len() = 3
s[0], s[1], s[2] = 65,0,66See also
readblob | Reads up to size bytes and returns them as a blob |
tostring | Allows calling .tostring() on blob instances (bypassing _get) |
| blob | class index |