Subject: Re: [Plt] number->string
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 2000/07/23
Newsgroups: comp.lang.scheme
Message-ID: <8ldidh$88h0a$1@fido.engr.sgi.com>
Noel Welsh  <noelw@dai.ed.ac.uk> wrote:
+---------------
| erich <socrates314@my-deja.com> writes:
| > I'm trying to implement a byte-based network protocol in mzScheme. For
| > this, I need to convert between long<-->4 byte string and short<-->2 byte
| > string in the order msb..lsb.
| 
| I've had the same problem you've encountered. I've written code to
| read all primitive integer and floating point types, but never cleaned
| it up enough to release it. Below is the Scheme code I used (I needed
| a bit of C to read floats and doubles)...
+---------------

There's also some code for reading binary numbers in various precisions
(though not floats and doubles) in the PLT distribution (which you should
have if you loaded the entire DrScheme release and not just core MzScheme)
in ".../plt/collects/net/dnsr.ss", which is the guts of the "net/dns.ss"
library:

  (define (number->octet-pair n)
    (list (integer->char (arithmetic-shift n -8))
          (integer->char (modulo n 256))))

  (define (octet-pair->number a b)
    (+ (arithmetic-shift (char->integer a) 8)
       (char->integer b)))

  (define (octet-quad->number a b c d)
    (+ (arithmetic-shift (char->integer a) 24)
       (arithmetic-shift (char->integer b) 16)
       (arithmetic-shift (char->integer c) 8)
       (char->integer d)))

The other code there (and in the other plt/collects/net/ libraries)
demonstrates one style (though certainly not the only reasonable one) of
how one might interact with external net entities from within MzScheme.


-Rob

-----
Rob Warnock, 41L-955		rpw3@sgi.com
Applied Networking		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043