[racket] integer->bytestring for a single byte?

From: Ryan Culpepper (ryan at cs.utah.edu)
Date: Wed Nov 7 22:45:28 EST 2012

On 11/07/2012 10:12 PM, Paul Meier wrote:
> Hi friends,
>
> I'm trying to write some integer values to a socket, and normally I'm
> generating the bytestring with integer->integer-bytes
> <http://docs.racket-lang.org/reference/generic-numbers.html?q=bytes#(def._((quote._~23~25kernel)._integer-~3einteger-bytes))>,
> and reading values on the other end with integer-bytes->integer
> <http://docs.racket-lang.org/reference/generic-numbers.html?q=bytes#(def._((quote._~23~25kernel)._integer-bytes-~3einteger))>,
> consequently.
>
> Sometimes, however, I'd like to send or read only a single byte, but the
> contracts for those functions will only format the numbers if the
> bytestring buffer that is written to or read from is 2, 4, or 8.
>
> Is there a way to achieve a similar result (a machine-formatted
> bytestring) for values that fit in a byte? And if enthusiastic pedants
> want are feeling instructive to an willing learner, why are those values
> in the contract?

If it's an unsigned byte, you can just call the 'bytes' function:

 > (bytes 65)
#"A"

Ryan


Posted on the users mailing list.