Skip to main content

Module bytes

Module bytes 

Source
Expand description

The bit/trit boundary (D-04).

One byte maps to one tryte holding its unsigned value, 0 to 255.

§What this is not

This is the octet boundary, not the arithmetic path. It applies where data crosses between the machine and a byte-oriented host: a file read through the block device, a character written to the console, a network frame.

Native programs never call these functions. They compute in trytes, words and double words, whose ranges are set by D-02 and have nothing to do with 255:

  tryte        9 trits    +/- 9,841
  word        27 trits    +/- 3,812,798,742,493
  double word 54 trits    +/- 29,074,868,501,520,029,845,195,084

§Direction matters

Ingress is total. Every byte value has a tryte, so byte_to_tryte cannot fail and no information is lost.

Egress is partial. tryte_to_byte fails outside 0..=255 because there is no octet to produce. This is serialisation refusing to truncate, not a narrowing of the machine’s arithmetic. A binary host has the same boundary in the same place: putchar takes an int and a caller that passes 300 has to decide what it meant, and encoding a large value as several bytes is the answer in both systems.

§Binary program types

Running a program written for a binary machine is a separate question, answered by D-16 rather than here. In outline, i32 and u32 fit in one word and i64 and u64 fit in one double word, so the values are carried exactly. What costs anything is wraparound: reducing modulo 2^64 is not natural in base 3, so a translated program pays only where it relies on unsigned overflow.

Constants§

MAX_BYTE_VALUE
Largest tryte value with a byte representation.

Functions§

byte_to_tryte
Converts a byte to the tryte holding its unsigned value.
bytes_to_trytes
Converts a slice of bytes to trytes.
tryte_to_byte
Converts a tryte back to a byte.
trytes_to_bytes
Converts trytes back to bytes, failing on the first value out of range.