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] |
Maciej Stachowiak <mstachow@mit.edu> wrote: > 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. ;The easiest for you is to use the uniform vector type. A very useful ;data type available in scm/guile. In guile there are also structs which ;allows for more complicated binary patterns. ;A very simple test program that writes and read two 32 bit numbers ;Make a uniform vector of type and size you want. ;The -1 is a prototype which means signed integer, 1 is unsigned (define twonumber (make-uniform-vector 2 -1)) ;Fill them with some numbers (array-set! twonumber 255 0) (array-set! twonumber 65280 1) ;Write the numbers binary to file (define out (open-output-file "twonumber")) (uniform-vector-write twonumber out) (close-output-port out) ;Read them back again in another vector (define numbers (make-uniform-vector 2 -1)) (define in (open-input-file "twonumber")) (close-input-port out) (uniform-vector-read! numbers in) ; Try (apropos "uniform-vector") ; and check docs about uniform vectors. If you are communicating data ; between different endian machines the cleanest way is to use an ; intermediate file for the conversion. Best reagards Roland ------------------------------+---------------------+----------------- Roland Orre | O---O---O Studies of| orre@nada.kth.se SANS, NADA, KTH | |\ /|\ / Artificial| S-100 44 Stockholm, Sweden | O-O-O-O Neural |Wph:+46 8 7906984 ------------------------------+ |/ \ /| Systems |Fax:+46 8 7900930 Dept. of Computing Science | O---O-O +---------|Mob:+46 70 8269748 Royal Institute of Technology | |http://www.nada.kth.se/~orre ------------------------------+----------+----------------------------