bytevector->u8-list(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

✨ bytevector->u8-list, bytevector->sint-list, bytevector->uint-list ✨

okay bestie, welcome to the Scheme Programmer's Manual πŸŽ€ β€” we're talking about three little icon procedures today, and honestly? they ate.


πŸ’– LIBRARY πŸ’–

(import (rnrs))                     ;R6RS
(import (rnrs bytevectors))         ;R6RS

so you can pull these from either one, no cap 🫢


🌸 SYNOPSIS 🌸

(bytevector->u8-list bytevector)
(bytevector->sint-list bytevector endianness size)
(bytevector->uint-list bytevector endianness size)

like, that's the whole vibe check right there πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so these procedures convert a bytevector into a list of integer objects πŸŽ€ β€” and they do it according to size and endianness (iykyk). the integers land in the same order as the data sitting in bytevector, so no sneaky reordering, fr fr.

and bytevector->u8-list? she's literally the same as bytevector->uint-list with a size of 1. periodt. she returns a newly allocated list of the octets of bytevector, in the exact same order πŸ’œ


⭐ RETURN VALUES ⭐

returns a newly allocated list of exact integer objects πŸ’– (new list, fresh out the oven, no shared structure shenanigans)


πŸ”₯ EXAMPLES πŸ”₯

first one, beware β€” this is the example straight from the page, shameless and iconic:

(let ((b (make-bytevector 4 -127)))
  (bytevector->u8-list b))
        => #vu8(129 129 129 129)

next up, the signed girly with (endianness little):

(let ([b (u8-list->bytevector
           '(#x1 #x2 #x3 #xFF #x1 #x2 #x1 #x2))])
  (bytevector->sint-list b (endianness little) #x2))
        => (#x201 #x-FD #x201 #x201)

and the unsigned version, same bytes, different energy:

(let ([b (u8-list->bytevector
           '(#x1 #x2 #x3 #xFF #x1 #x2 #x1 #x2))])
  (bytevector->uint-list b (endianness little) #x2))
        => (#x201 #xFF03 #x201 #x201)

see how #x-FD vs #xFF03 be doing the most? that's the whole signed/unsigned main character energy right there 😭✨


πŸ’€ ERRORS πŸ’€

these procedures can raise exceptions with the following condition types:

  • &assertion (R6RS) β€” wrong number of arguments got passed, or an argument was outside its domain. in particular: size must be a positive exact integer object, and bytevector must be evenly divisible by size. so like, don't try to be cute and pass a weird size, she WILL throw πŸ™ˆ

🌈 SEE ALSO 🌈

go read those too, they're besties with these procedures 🧚


πŸ‘‘ STANDARDS πŸ‘‘

R6RS ⭐


πŸ“ AUTHORS πŸ“

this page is part of the scheme-manpages project πŸ’– it includes materials from the RnRS documents, and more info can be found at https://github.com/schemedoc/manpages/ β€” go show them some love 🫢


and that's the tea, bestie β€” scheme-manpages, 2021-02-28, bytevector->u8-list(3scm), signing off πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from