ZetCode

Scheme bytevectors

last modified November 24, 2026 Guile 3.0

A bytevector is a fixed-length sequence of bytes. Where a vector holds any Scheme values, a bytevector holds only integers from 0 to 255, stored compactly as raw memory. Bytevectors represent binary data: encoded text, file formats, network messages, and checksums. This article creates and changes bytevectors, converts text to and from UTF-8, and reads integers and floating-point numbers from groups of bytes. The examples run with GNU Guile 3.0.7.

Creating bytevectors

In a Guile script the bytevector procedures come from the R6RS module (rnrs bytevectors); without it, even make-bytevector is unbound. A bytevector literal is written #vu8(...).

creating.scm
(use-modules (rnrs bytevectors))

(define buffer (make-bytevector 6 0))
(define header #vu8(137 80 78 71))
(define bytes (u8-list->bytevector (list 1 2 3 255)))

(write buffer)
(newline)
(write header)
(newline)
(write bytes)
(newline)
(write (list (bytevector-length header) (bytevector? header) (vector? header)))
(newline)
(write (bytevector->u8-list header))
(newline)

make-bytevector creates a bytevector of a given length filled with one byte value. The literal header holds the first four bytes of a PNG file. u8-list->bytevector and bytevector->u8-list convert between bytevectors and lists of byte values. A bytevector is a type of its own, not a vector.

$ guile creating.scm
#vu8(0 0 0 0 0 0)
#vu8(137 80 78 71)
#vu8(1 2 3 255)
(4 #t #f)
(137 80 78 71)

Reading and writing bytes

bytevector-u8-ref reads the byte at an index as an unsigned integer, and bytevector-u8-set! stores one. The u8 in the names stands for an unsigned 8-bit integer.

bytes.scm
(use-modules (rnrs bytevectors))

(define flags (make-bytevector 4 0))

(bytevector-u8-set! flags 0 1)
(bytevector-u8-set! flags 1 128)
(bytevector-u8-set! flags 3 255)
(write flags)
(newline)
(write (bytevector-u8-ref flags 1))
(newline)

(define (byte-sum bv)
  (let loop ((i 0) (sum 0))
    (if (= i (bytevector-length bv))
        sum
        (loop (+ i 1) (+ sum (bytevector-u8-ref bv i))))))

(write (byte-sum flags))
(newline)

(define (invert! bv)
  (do ((i 0 (+ i 1)))
      ((= i (bytevector-length bv)))
    (bytevector-u8-set! bv i (- 255 (bytevector-u8-ref bv i)))))

(invert! flags)
(write flags)
(newline)

The program sets three of the four bytes and reads one back. byte-sum adds all bytes in a named let, and invert! replaces each byte by 255 minus its value, which flips all eight bits. The loops are the same as for vectors; only the access procedures differ.

$ guile bytes.scm
#vu8(1 128 0 255)
128
384
#vu8(254 127 255 0)

Byte range

A byte holds a value from 0 to 255. Storing a larger value does not wrap around; it is an error.

byterange.scm
(use-modules (rnrs bytevectors))

(define flags (make-bytevector 4 0))

(bytevector-u8-set! flags 0 255)
(write flags)
(newline)
(bytevector-u8-set! flags 1 256)

255 is the largest value that fits. 256 needs nine bits, and bytevector-u8-set! stops the program with an out of range error for its third argument. The backtrace is omitted.

$ guile byterange.scm
#vu8(255 0 0 0)
byterange.scm:8:0: In procedure bytevector-u8-set!: Argument 3 out of range: 256

Signed bytes

The same eight bits can also be read as a signed integer from -128 to 127. bytevector-s8-ref and bytevector-s8-set! use the two's complement representation, in which the bytes from 128 to 255 stand for the negative numbers.

signed.scm
(use-modules (rnrs bytevectors))

(define data #vu8(0 1 127 128 200 255))

(write (bytevector->u8-list data))
(newline)
(write (map (lambda (i) (bytevector-s8-ref data i)) (iota 6)))
(newline)

(define temps (make-bytevector 3 0))

(bytevector-s8-set! temps 0 -12)
(bytevector-s8-set! temps 1 25)
(bytevector-s8-set! temps 2 -1)
(write temps)
(newline)
(write (list (bytevector-s8-ref temps 0) (bytevector-s8-ref temps 2)))
(newline)

The values up to 127 read the same in both views. The byte 128 is -128 as a signed number, 200 is -56, and 255 is -1. Storing -12 as a signed byte puts the byte 244 into the bytevector, which is 256 minus 12. A bytevector does not remember how its bytes were written, so the program must read them with the same view.

$ guile signed.scm
(0 1 127 128 200 255)
(0 1 127 -128 -56 -1)
#vu8(244 25 255)
(-12 -1)

Text as bytes

Strings hold characters, and files and networks carry bytes. UTF-8 is the usual encoding between the two. string->utf8 encodes a string into a bytevector and utf8->string decodes one.

text.scm
(use-modules (rnrs bytevectors))

(define (show word)
  (let ((bv (string->utf8 word)))
    (write word)
    (display " characters: ")
    (display (string-length word))
    (display ", bytes: ")
    (display (bytevector-length bv))
    (display " ")
    (write bv)
    (newline)))

(for-each show (list "cat" "Žltá" "λ" "€" "日本"))

(define encoded #vu8(80 108 122 101 197 136))

(write (utf8->string encoded))
(newline)
(write (string=? (utf8->string (string->utf8 "čaj")) "čaj"))
(newline)

ASCII characters take one byte each. Ž and á take two bytes, λ two, the euro sign three, and each of the two Japanese characters three, so the byte length of a string is generally larger than its character count. utf8->string decodes the six bytes of encoded into the five characters of "Plzeň". Encoding and decoding returns the original string.

$ guile text.scm
"cat" characters: 3, bytes: 3 #vu8(99 97 116)
"Žltá" characters: 4, bytes: 6 #vu8(197 189 108 116 195 161)
"λ" characters: 1, bytes: 2 #vu8(206 187)
"€" characters: 1, bytes: 3 #vu8(226 130 172)
"日本" characters: 2, bytes: 6 #vu8(230 151 165 230 156 172)
"Plzeň"
#t

A hex dump

Binary data is usually displayed in hexadecimal, two digits per byte. number->string with radix 16 converts a byte, and a leading zero pads the values below 16.

hexdump.scm
(use-modules (rnrs bytevectors))

(define (byte->hex b)
  (let ((s (number->string b 16)))
    (if (< b 16) (string-append "0" s) s)))

(define (hex-string bv)
  (string-join (map byte->hex (bytevector->u8-list bv)) " "))

(write (hex-string #vu8(0 15 16 255)))
(newline)

(define (hex-dump bv width)
  (do ((i 0 (+ i 1)))
      ((= i (bytevector-length bv)) (newline))
    (when (zero? (modulo i width))
      (unless (zero? i) (newline))
      (display (byte->hex i))
      (display ":"))
    (display " ")
    (display (byte->hex (bytevector-u8-ref bv i)))))

(hex-dump (string->utf8 "Hello there, Žilina!") 8)

byte->hex formats one byte, and hex-string joins the bytes of a short bytevector with spaces. hex-dump prints width bytes per line and starts each line with the offset of its first byte. The dump of the UTF-8 text shows the two bytes c5 bd of the letter Ž.

$ guile hexdump.scm
"00 0f 10 ff"
00: 48 65 6c 6c 6f 20 74 68
08: 65 72 65 2c 20 c5 bd 69
10: 6c 69 6e 61 21

Multi-byte integers

Larger integers occupy several bytes. The order of those bytes is the endianness: big-endian puts the most significant byte first, little-endian the least significant. The u16, u32, and s32 procedures take the endianness as their last argument, written (endianness big) or (endianness little).

integers.scm
(use-modules (rnrs bytevectors))

(define bv (make-bytevector 4 0))

(bytevector-u16-set! bv 0 258 (endianness big))
(bytevector-u16-set! bv 2 258 (endianness little))
(write bv)
(newline)

(write (list (bytevector-u16-ref bv 0 (endianness big))
             (bytevector-u16-ref bv 0 (endianness little))))
(newline)

(define number (make-bytevector 4 0))
(bytevector-u32-set! number 0 3000000000 (endianness big))
(write number)
(newline)
(write (bytevector-u32-ref number 0 (endianness big)))
(display " ")
(write (bytevector-s32-ref number 0 (endianness big)))
(newline)

(write (native-endianness))
(newline)
(bytevector-u32-native-set! number 0 1)
(write number)
(newline)

258 is 1 × 256 + 2, so it is stored as the bytes 1 and 2 in big-endian order and as 2 and 1 in little-endian order. Reading the first two bytes with the wrong endianness gives 513. The number three billion fills four bytes; read as a signed 32-bit integer, the same bytes give a negative number, because the highest bit is set. native-endianness returns the order of the machine, little on x86-64, and the native procedures use it.

$ guile integers.scm
#vu8(1 2 2 1)
(258 513)
#vu8(178 208 94 0)
3000000000 -1294967296
little
#vu8(1 0 0 0)

Floating-point numbers

bytevector-ieee-double-set! and bytevector-ieee-double-ref store and read an inexact number in the eight-byte IEEE 754 format. The single variants use the four-byte format, which has less precision.

floats.scm
(use-modules (rnrs bytevectors))

(define bv (make-bytevector 8 0))

(bytevector-ieee-double-set! bv 0 1.5 (endianness big))
(write bv)
(newline)
(write (bytevector-ieee-double-ref bv 0 (endianness big)))
(newline)

(define single (make-bytevector 4 0))

(bytevector-ieee-single-set! single 0 0.1 (endianness big))
(write single)
(newline)
(write (bytevector-ieee-single-ref single 0 (endianness big)))
(newline)

(bytevector-ieee-double-set! bv 0 0.1 (endianness big))
(write (bytevector-ieee-double-ref bv 0 (endianness big)))
(newline)

1.5 is exactly representable, and its eight bytes start with 63 and 248 (3f f8 in hexadecimal). 0.1 is not exact in binary. In the four-byte format it is rounded more coarsely, so reading it back gives 0.10000000149011612; in the eight-byte format it survives the round trip unchanged.

$ guile floats.scm
#vu8(63 248 0 0 0 0 0 0)
1.5
#vu8(61 204 204 205)
0.10000000149011612
0.1

Copying

bytevector-copy creates a new bytevector with the same bytes. bytevector-copy! copies a range of bytes from one bytevector into another at a given position; its arguments are the source, the source start, the target, the target start, and the number of bytes.

copying.scm
(use-modules (rnrs bytevectors))

(define source #vu8(10 20 30 40 50))
(define copy (bytevector-copy source))

(bytevector-u8-set! copy 0 99)
(write (list source copy))
(newline)

(define target (make-bytevector 8 0))
(bytevector-copy! source 1 target 4 3)
(write target)
(newline)

(define (subbytes bv start end)
  (let ((part (make-bytevector (- end start))))
    (bytevector-copy! bv start part 0 (- end start))
    part))

(write (subbytes source 2 4))
(newline)

(write (list (equal? source (bytevector-copy source))
             (bytevector=? source copy)))
(newline)
(bytevector-fill! target 7)
(write target)
(newline)

Changing copy leaves source unchanged. bytevector-copy! copies three bytes starting at index 1 of the source to index 4 of the target. subbytes uses it to extract a range into a new bytevector. Bytevectors can be compared with equal? or with bytevector=?, and bytevector-fill! sets every byte.

$ guile copying.scm
(#vu8(10 20 30 40 50) #vu8(99 20 30 40 50))
#vu8(0 0 0 0 20 30 40 0)
#vu8(30 40)
(#t #f)
#vu8(7 7 7 7 7 7 7 7)

Packing a binary header

Binary file formats often start with a fixed-size header. The program below packs fields of different sizes into a 16-byte header and unpacks them again: a two-byte magic marker, a 16-bit version, a 32-bit count, and a double.

packing.scm
(use-modules (rnrs bytevectors))

;; magic "ZC" (2 bytes), version u16, item count u32, ratio as double
(define header-size 16)

(define (pack-header version count ratio)
  (let ((bv (make-bytevector header-size 0)))
    (bytevector-u8-set! bv 0 (char->integer #\Z))
    (bytevector-u8-set! bv 1 (char->integer #\C))
    (bytevector-u16-set! bv 2 version (endianness big))
    (bytevector-u32-set! bv 4 count (endianness big))
    (bytevector-ieee-double-set! bv 8 ratio (endianness big))
    bv))

(define (unpack-header bv)
  (if (and (= (bytevector-u8-ref bv 0) (char->integer #\Z))
           (= (bytevector-u8-ref bv 1) (char->integer #\C)))
      (list (cons 'version (bytevector-u16-ref bv 2 (endianness big)))
            (cons 'count (bytevector-u32-ref bv 4 (endianness big)))
            (cons 'ratio (bytevector-ieee-double-ref bv 8 (endianness big))))
      #f))

(define packed (pack-header 3 70000 0.75))

(write packed)
(newline)
(write (unpack-header packed))
(newline)
(write (unpack-header (make-bytevector header-size 0)))
(newline)

pack-header stores each field at a fixed offset, all in big-endian order, which is common in file formats. The magic bytes are the codes of the letters Z and C. unpack-header checks the magic bytes first and returns an association list of the fields, or #f for data that does not start with the marker. The count 70000 appears in the bytes as 0 1 17 112, which is 1 × 65536 + 17 × 256 + 112.

$ guile packing.scm
#vu8(90 67 0 3 0 1 17 112 63 232 0 0 0 0 0 0)
((version . 3) (count . 70000) (ratio . 0.75))
#f

A checksum

A checksum summarises a block of bytes in a single number that changes when the data changes. Adler-32, used by the zlib compression format, keeps two running sums modulo 65521 and combines them into one 32-bit value.

checksum.scm
(use-modules (rnrs bytevectors))

(define (adler32 bv)
  (let loop ((i 0) (a 1) (b 0))
    (if (= i (bytevector-length bv))
        (+ (* b 65536) a)
        (let ((a2 (modulo (+ a (bytevector-u8-ref bv i)) 65521)))
          (loop (+ i 1) a2 (modulo (+ b a2) 65521))))))

(define (checksum text)
  (number->string (adler32 (string->utf8 text)) 16))

(display (checksum "Wikipedia"))
(newline)
(display (checksum "wikipedia"))
(newline)
(display (checksum ""))
(newline)

(define (checksum->bytes n)
  (let ((bv (make-bytevector 4 0)))
    (bytevector-u32-set! bv 0 n (endianness big))
    bv))

(write (checksum->bytes (adler32 (string->utf8 "Wikipedia"))))
(newline)

adler32 walks the bytes with two accumulators: a is one plus the sum of the bytes, and b is the sum of all the intermediate values of a. checksum encodes a string to UTF-8 first. The checksum of "Wikipedia" is the well-known test value 11e60398; changing a single letter changes it completely, and the empty input gives 1. checksum->bytes stores the result in four bytes, as a file format would.

$ guile checksum.scm
11e60398
130603b8
1
#vu8(17 230 3 152)

Source

Bytevectors — GNU Guile Reference Manual

In this article we have created bytevectors, read and written unsigned and signed bytes, encoded and decoded UTF-8 text, printed a hex dump, stored 16-bit and 32-bit integers in both byte orders, stored floating-point numbers, copied byte ranges, packed and unpacked a binary header, and computed an Adler-32 checksum. The vectors article covers general vectors, and the strings article works with text as characters.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Scheme tutorials.