[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: binary representation of numbers
From: |
Mikael Djurfeldt |
Subject: |
Re: binary representation of numbers |
Date: |
Mon, 03 Feb 2003 16:39:41 +0100 |
User-agent: |
Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.2 (i386-pc-linux-gnu) |
Rohan Drape <address@hidden> writes:
> Hello All,
>
> I have searched the documentation but cannot find procedures to convert
> between scheme numbers and common machine byte representations. I need
> something equivalent to the PLT procedures:
>
> (integer-byte-string->integer string signed? [big-endian?])
> (integer->integer-byte-string n size-n signed? [big-endian? to-string])
> (floating-point-byte-string->real string [big-endian?])
> (floating-point-byte-string->real string [big-endian?])
>
> which assume of course that strings are byte vectors. These are required to
> implement a simple byte protocol over UDP. I am sure this is `under my nose'
> but I cannot find it, any pointers would be appreciated.
My suspicion is that you can't do this in a reasonable way in Guile.
>From the C level it's easy enough to do it, but not on the Scheme
level. Someone should implement these primitives for Guile.
There is a clumsy way to do it, though. It's based on the use of
string-ports and uniform-vector-read/write.
This is a hint of how to do it. It's an incomplete implementation of
integer->integer-byte-string:
(define (integer->integer-byte-string n size-n signed?)
(with-output-to-string
(lambda ()
(uniform-vector-write
(list->uniform-vector (select-proto size-n signed?) (list n))))))
(define (select-proto . spec)
(cdr (assoc spec '(((2 #t) . s)
((4 #f) . 1)
((4 #t) . -1)
((8 #t) . l)))))