bytevector->string(3scm)

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

✨ bytevector->string β€” the transcode-a-bytevector-to-a-string glow-up ✨

okay so real quick, this page lives in the Scheme Programmer's Manual (yes, bytevector->string(3scm), that one πŸ’…) and it's genuinely one of the most useful little guys in the whole I/O lineup. let's get into it 🫢


πŸ’– LIBRARY πŸ’–

import it from either of these bad boys, depending on your vibe:

(import (rnrs))                     ;R6RS
(import (rnrs io ports))            ;R6RS

like, both work. pick your fighter 🌸


🌈 SYNOPSIS 🌈

(bytevector->string bytevector transcoder)

two args, no drama. bytevector and transcoder. that's the whole signature, bestie ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so what it does is: it takes bytevector and transcodes it according to the input direction of transcoder, and hands you back the resulting string. that's it. that's the whole personality πŸ’œ

(and yes, "input direction" is doing real work in that sentence β€” it's the input side of the transcoder that's in charge here, not the output side. iykyk πŸ‘‘)


πŸŽ€ RETURN VALUES πŸŽ€

Returns a single value: a string. one value. not a list, not a couple of things, just the string. iconic behavior, honestly πŸ’…


⭐ EXAMPLES ⭐

ok these are so clean, look:

(bytevector->string #vu8(81 117 117 120) (native-transcoder))
        => "Quux"

literally just bytes in, "Quux" out. ate. πŸ”₯

and then the spicy one where things go sideways:

(bytevector->string #vu8(#xff)
                    (make-transcoder (utf-8-codec)
                                     (native-eol-style)
                                     (error-handling-mode raise)))
        => &i/o-decoding

yep β€” #xff is not valid UTF-8 and you said raise, so it raises. that's the deal you made 😭 (more on that below πŸ‘‡)


🌸 APPLICATION USAGE 🌸

this procedure is the more general, more flexible alternative to utf8->string(3scm). like, utf8->string is cool but this one lets you pick the transcoder, so it's giving main character energy 🌟


πŸ’€ COMPATIBILITY πŸ’€

this procedure is unique to R6RS. periodt. if you're on something else, sorry bestie πŸ™ˆ


🚨 ERRORS 🚨

it can raise exceptions with these condition types, so know them before you get humbled:

  • &assertion (R6RS) β€” you passed the wrong number of arguments, or an argument was outside its domain. classic. πŸ’€
  • &i/o-decoding (R6RS) β€” a sequence of bytes showed up that could not be translated into a string, and the transcoder's error-handling-mode is raise. like if you'd set it to something chiller, it wouldn't be raising at you. 😭

🧚 SEE ALSO 🧚

go check these out, they're all in the same friend group:

(and yeah, string->bytevctor is spelled exactly like that on the page β€” not me, blame the original πŸ™ˆ copy it as-is if you're grepping for it!)


πŸ‘‘ STANDARDS πŸ‘‘

R6RS. that's the whole section. slay. ✨


πŸ“ HISTORY πŸ“

This procedure first appeared in R6RS, as part of the redesigned I/O system. so it's not ancient lore, it's a glow-up baby 🌱


πŸ’œ AUTHORS πŸ’œ

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.


ok tiny recap so it sticks: bytevector + transcoder (input direction) β†’ one string. watch out for &assertion and &i/o-decoding. and don't forget it's R6RS-only, mwah πŸ’‹

that's the whole page, bestie β€” go transcode something πŸ’–

later, and remember: it's bytevector->string(3scm), from the Scheme Programmer's Manual, dated 2020-05-14. πŸ¦‹

πŸ“¦ where this page comes from