Quirrel4.41.0

iostream.blob.as_string

Method of blob, from module "iostream"
instance.as_string(): string

Returns the blob contents as a string

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

let cargo = blob(0)
cargo.writestring("ammo")
println("cargo.as_string() =", cargo.as_string())
Output:
cargo.as_string() = ammo

Return 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

examples/iostream/blob/as_string.nut
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,66

See also

readblobReads up to size bytes and returns them as a blob
tostringAllows calling .tostring() on blob instances (bypassing _get)
blobclass index