std.bytes
std.bytes works with raw byte data: a buffer of numbers between 0 and 255. It is the building block for everything that is not text — image files, data over the network, and what cryptography operates on.
std.bytes works with raw byte data: a buffer of numbers between 0 and 255. It is the building block for everything that is not text — image files, data over the network, and the content cryptography operates on.
The module bridges a list of byte values and a buffer, lets you read size and content, and interpret bytes as text when that makes sense. If you need to handle binary content safely, this is the layer you build on.
When to use it
Use std.bytes when you handle something that is not text: reading an image file, taking in raw data over the network, or assembling bytes from a list. fra_liste and til_liste bridge a byte list and a buffer.
Getting started
bruk std.bytes som bytes
funksjon start() -> heiltall {
la buffer = bytes.fra_liste([72, 101, 105]) // H, e, i
skriv(bytes.til_tekst(buffer)) // "Hei"
skriv(tekst(bytes.storleik(buffer))) // "3"
returner 0
}A buffer is built from a list of numeric values (0–255). til_tekst interprets them as text, and storleik returns the number of bytes.
The functions
The module has builders (ny, tom, fra_liste), readers (til_liste, til_tekst, utsnitt_til_tekst, storleik) and a check (er_bytes).
ny(storleik: heiltall, fyll: heiltall)— Makes a buffer of a given size, filled with a starting value.tom()— Makes an empty buffer.fra_liste(verdiar: liste_heltall)— Makes a buffer from aliste_heltallof byte values.til_liste(buffer) -> liste_heltall— Turns a buffer into aliste_heltall.utsnitt_til_tekst(buffer, start: heiltall, tal: heiltall) -> tekst— Interprets part of the buffer (fromstart,talbytes) as text.til_tekst(buffer) -> tekst— Interprets the whole buffer as text.er_bytes(verdi) -> boolsk— True if the value is a byte buffer.storleik(buffer) -> heiltall— Number of bytes in the buffer.produksjonsstatus() -> ordbok_tekst— Internal status information about the byte module.
Good to know
- The values in a buffer are numbers between 0 and 255; going outside is an error in the data, not something the module fixes.
til_tekstassumes the bytes are actually text (UTF-8); raw binary data should not be interpreted as text.- Use
er_byteswhen you are unsure whether a value is a buffer before you process it.
std.bytes is one of the modules in the standard library. Everything ships with the runtime — no installation, no external dependencies. See also Documentation for the language and the runtime.
Related
- std.tekststd.tekst is the toolbox for everything to do with text — searching and checking, splitting and joining, trimming whitespace, changing case, and converting to and from numbers.
- std.httpstd.http is a client for talking to other services over HTTP and HTTPS. You can fetch data, send data, and interpret the response — including JSON — without building the request by hand.
- std.dbstd.db gives you access to databases with a small, familiar set of functions: open a connection, run SQL, fetch values and rows, and control transactions.