[plt-scheme] integer->byte?

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sat Mar 8 18:24:39 EST 2008

tom sgouros wrote:
> Hello all:
> 
> Part of the old code I'm updating depended on passing around lists of
> char data.  What I was really doing was sending out lists of bytes and
> receiving other lists of bytes from a variety of serial devices.  (I had
> been using strings, but found that manipulating lists of bytes was less
> ambiguous in important cases, and I didn't have to keep track of lengths
> and terminating characters.)
> 
> I see all the byte string manipulation things, and those look ok to me.
> But what I really want is to be able to manipulate a list of 8-bit
> values, to send to and receive from a serial device.  For example, how
> do I turn the literal value #x7f into an eight-bit value?  There is no
> integer->byte command.
> 
> To be more specific, I used to have this:
> 
>   (define header (list (integer->char #x7e)))
>   (define middle (list (integer->char #x05)
>                        (integer->char #x47)))
>   (define footer (list (integer->char #xff)))
> 
> But I can't see how to write this in the brave new byte world.  Can
> anyone suggest how something like this should read?

A byte is just an exact integer in [0, 255] (see the definition of 
'byte?' in the manual). In your code above, just remove the calls to 
'integer->char':

   ;; header, middle, footer : (listof byte?)

   (define header (list #x7e))
   (define middle (list #x05
                        #x47))
   (define footer (list #xff))

If you used 'write-char' to send the data before, you should change that 
to 'write-byte'. If you called 'list->string' and then sent the string, 
you should change that to 'list->bytes' (note: 'bytes', not 'byte') and 
then use 'write-bytes'.

Ryan

> The (list->byte) function doesn't really do what I want, since it
> creates something comparable to a string and not something list-like. 
> 
> Many thanks (again),
> 
>  -tom
> 



Posted on the users mailing list.