In one sentence
Bytes is a sequence of octets, numbers from 0 to 255; it is used to work with binary data, such as the exact content of a file.
3.14.1 How to write itDefine «name» As Bytes <- Bytes ( ) ;
Define «name» As Bytes <- bytesFromText ( «text» ) ;
«name» . at ( «position» )
«name» . segment ( «start» , «count» ) 3.14.2 How it worksA text is a row of characters; bytes are the way that text is stored in a file or travels over the network. Converting a text to bytes uses the UTF-8 encoding. Letters without accents take one octet, but ñ takes two.
Program The octets of a word Algorithm Octetos
Define datos As Bytes <- bytesFromText ( "año" ) ;
Write datos . length ( ) , " " , datos . at ( 0 ) , " " , datos . at ( 1 ) , " " , datos . at ( 2 ) ;
Define vacio As Bytes <- Bytes ( ) ;
Write vacio . length ( ) ;
EndAlgorithm The program finishes normally.
Figure 3.14-1. "año" has three characters but four octets: ñ is encoded with two.At the top, the three characters of año: a, ñ and o. Below, their four octets: 97 for a, 195 and 177 for ñ, and 111 for o.
Decoding goes the other way and returns a Result , because not every byte sequence is a valid text:
Program From bytes to text Algorithm Decodificar
Define datos As Bytes <- bytesFromText ( "año" ) ;
Switch datos . utf8Text ( ) Do
Success ( texto ) : Write texto , " (" , length ( texto ) , " caracteres)" ;
Otherwise : Write "no es UTF-8" ;
EndSwitch
EndAlgorithm The program finishes normally.
Limit
Bytes is immutable : taking a segment or joining return new sequences. That is why it can move between tasks.Reading position i returns an Integer between 0 and 255, with i from 0. A segment must stay inside the sequence; otherwise, bytesRangeOutOfBounds . Encoding adds no byte order mark (BOM). Common mistakes
A segment that goes past the sequence stops the program at the call:
Program A segment that is too long Algorithm Segmento
Define datos As Bytes <- bytesFromText ( "hola" ) ;
Write datos . segment ( 1 , 5 ) . length ( ) ;
EndAlgorithm Diagnostics
bytesRangeOutOfBounds · error · 3:9 · The segment exceeds the bounds of BytesThe program finishes with an error.
Under the hood
A byte sequence lives on the heap and never changes, so several variables can share it without copying it. Segments and joins create new sequences instead of modifying the original.
Intermediate representation
module Octetos en
class "$effect:Bytes" {}
func suspendible @main() -> unit {
b0() mass 4:
stmt.enter @2:3-2:48
%0 = value.undefined str @2:28-2:48
%1 = const.str "año" @2:42-2:47
%2 = cell.local obj<"$effect:Bytes"> @2:28-2:48
%3 = effect_call %0, bytes_from_text, obj<"$effect:Bytes">, n6, %1, %2 @2:28-2:48
%4 = cell.load_raw %2 @2:28-2:48
stmt.exit @2:3-2:48
stmt.enter @3:3-3:77
%5 = cell.local i64 @3:9-3:23
%6 = effect_call %4, bytes_length, i64, n12, %5 @3:9-3:23
%7 = cell.load_raw %5 @3:9-3:23
%8 = const.char ' ' @3:25-3:28
%9 = const.i64 0 @3:39-3:40
%10 = cell.local i64 @3:30-3:41
%11 = effect_call %4, bytes_at, i64, n18, %9, %10 @3:30-3:41
%12 = cell.load_raw %10 @3:30-3:41
%13 = const.char ' ' @3:43-3:46
%14 = const.i64 1 @3:57-3:58
%15 = cell.local i64 @3:48-3:59
%16 = effect_call %4, bytes_at, i64, n24, %14, %15 @3:48-3:59
%17 = cell.load_raw %15 @3:48-3:59
%18 = const.char ' ' @3:61-3:64
%19 = const.i64 2 @3:75-3:76
%20 = cell.local i64 @3:66-3:77
%21 = effect_call %4, bytes_at, i64, n30, %19, %20 @3:66-3:77
%22 = cell.load_raw %20 @3:66-3:77
out.write %7, %8, %12, %13, %17, %18, %22, newline @3:3-3:77
stmt.exit @3:3-3:77
stmt.enter @4:3-4:35
%23 = value.undefined str @4:28-4:35
%24 = cell.local obj<"$effect:Bytes"> @4:28-4:35
%25 = effect_call %23, bytes_new, obj<"$effect:Bytes">, n37, %24 @4:28-4:35
%26 = cell.load_raw %24 @4:28-4:35
stmt.exit @4:3-4:35
stmt.enter @5:3-5:23
%27 = cell.local i64 @5:9-5:23
%28 = effect_call %26, bytes_length, i64, n43, %27 @5:9-5:23
%29 = cell.load_raw %27 @5:9-5:23
out.write %29, newline @5:3-5:23
stmt.exit @5:3-5:23
return @6:1-6:13
} In short
Bytes is an immutable sequence of octets from 0 to 255.Text is encoded to UTF-8; decoding returns a Result . Some characters take several octets. See also