Skip to content
NorscodeNorscode

std.bytes

ReferenceBy the Norscode project

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 a liste_heltall of byte values.
  • til_liste(buffer) -> liste_heltall — Turns a buffer into a liste_heltall.
  • utsnitt_til_tekst(buffer, start: heiltall, tal: heiltall) -> tekst — Interprets part of the buffer (from start, tal bytes) 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_tekst assumes the bytes are actually text (UTF-8); raw binary data should not be interpreted as text.
  • Use er_bytes when 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

Back to the overview