RE: Reading and writing double floats from binary files

"Roger Corman" <[email protected]> Thu, 7 Aug 2003 01:56:22 -0700
Newsgroups gmane.lisp.corman
Message-ID <000001c35cc1$c6bf3890$0500a8c0@roger>
Here are some functions to read/write double floats as 64-bit binary
values, low-byte first. This should help, or you can probably see how to
modify them for your purposes. The file sys/math.lisp contains the
helper functions (in assembler) and also the equivalent functions for
single-floats if your need those. I will include these in the next
release.

(in-package :ccl)

(defun double-float-to-bytes (double-float)
    (let ((bits (cl::%double-float-bits double-float))
          (bytes (make-array 8 :element-type 'unsigned-byte)))
        (dotimes (i 8)
            (setf (aref bytes i) (logand (ash bits (* i -8)) #xff)))
        bytes))


(defun bytes-to-double-float (bytes)
    (let ((bits 0))
        (dotimes (i 8)
            (incf bits (ash (aref bytes i) (* i 8))))
        (cl::%make-double-float bits)))

(defun write-double-float (double-float &optional (stream
*standard-output*))
    "Writes a double float as a 64-bit binary value, low byte first."
    (let ((bytes (double-float-to-bytes double-float)))
        (dotimes (i (length bytes))
            (write-byte (aref bytes i) stream))
        double-float))

(defun read-double-float (&optional (stream *standard-input*))
    "Reads and returns a double float from a stream, as a 64-bit binary
value (low-byte first)"
    (let ((bytes (make-array 8 :element-type 'unsigned-byte)))
        (dotimes (i 8)
            (setf (aref bytes i) (read-byte stream)))
        (bytes-to-double-float bytes)))

(export '(ccl::write-double-float ccl::read-double-float))


Roger


-----Original Message-----
From: Norbert Lehmann [mailto:[email protected]] 
Sent: Tuesday, August 05, 2003 1:44 AM
To: [email protected]
Subject: [cormanlisp] Reading and writing double floats from binary
files


Hello

I have a file which contains some double floats (8 bytes) and some
integer values (4 bytes). I would like to read these double floats and
the integer values, doing some computation and then finally I would like
to write another file containing again some double floats and some
integer values.

Therefore, how can I read double floats and integer values from a file
and how can I write double floats and integer values to a file ?

In CLisp, I have used the functions "read-integer", "read-float",
"write-integer" and "write-float" (see
http://clisp.cons.org/impnotes.html for a description of these
functions) for that purpose.

Thanks very much + wish you all a nice day
Norbert

-- 
                                 \|/
                                (o o)
+---------------------------oOO--(_)--OOo---------------------------+
Norbert Lehmann
Department of Informatics (DIUF)            phone: ++ 41 26 300 83 30
University of Fribourg                        fax: ++ 41 26 300 97 26
Rue Faucigny 2                       e-mail: [email protected]
CH-1700 Fribourg                    http://www-iiuf.unifr.ch/~lehmann
Switzerland
+-------------------------------------------------------------------+
                              (_)   (_)


To unsubscribe from this group, send an email to:
[email protected]

 

Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/ 



------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/SyjtlB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
[email protected]

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/