This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: reading/writing binary data


| I'd like to write a guile script to communicate with a program that
| reads and writes data in binary format - 32 bit numeric values are
| written as four bytes in host byte order. I can get Guile to read this
| in as a string of four chars, but is there a reasonably portable way
| to convert that into a Scheme number (and vice versa for communicating
| the other way)? I can think of the simple-minded approach of using
| char->integer on each byte, bitshifting and adding, but I'd rather not
| put knowledge of the host's endianness into the script, and I'm hoping
| there's some primitives that can help do it more efficiently, although
| I don't know of any.

Have a look at uniform-array-write and uniform-array-read!, e.g., for
double floats:

(define a-double-float 1/3)
(define dv (make-uniform-vector 1 a-double-float 1.0))
(define p (open-output-file "one"))
(uniform-array-write dv p)
(close-port p)
(uniform-vector-set! dv 0 0.0)
(define p (open-input-file "one"))
(uniform-vector-read! dv p)
(close-port p)