read-bytes

Function summary
define-endianess endian
define-input-stream input
define-output-stream output
ieee-float-reader type
ieee-float-writer type
read-signed-integer nb-bytes
read-unsigned-integer nb-bytes
string-code s
write-signed-integer number nb-bytes
write-unsigned-integer quantity nb-bytes
Macro summary
define-ieee-float name &key (sign-bits 1) (exponent-bits 8) (mantissa-bits 23) (bias 127) (associated-type 'float)
with-gensyms syms &body body
READ-BYTES    [Package]

Binary bytes reader.

This package provides a binary bytes reader that makes Lisp capable of reading binary encoded numbers, for example with C. It supports both big and little endian numbers.

There are two predefined types that are 'single-float and 'double-float that correspond to the C types. In order to read or write such numbers, you can use either the read-IEEE-single-float, write-IEEE-single-float, read-IEEE-double-float, and write-IEEE-double-float, or get the reader or write corresponding to a given type (previously defined). Let us illustrate this for further clarity.

Example of usage for predefined types, fetching the appropriate functions given a type:

    CL-USER> (with-open-file 
                  (s "test.bin" 
                     :direction :output 
                     :element-type '(unsigned-byte 8) 
                     :if-exists :supersede) 
     	       (define-endianess :little-endian) 
     	       (define-output-stream s) 
     	       (funcall (IEEE-float-writer 'double-float) 56.3332563322d0)) 
     NIL 
     CL-USER> (with-open-file  
                  (s "test.bin" 
                     :direction :input 
                     :element-type '(unsigned-byte 8)) 
     	       (define-endianess :little-endian) 
     	       (define-input-stream s) 
     	       (funcall (IEEE-float-reader 'double-float))) 
     56.3332563322d0 

Example of new number encoding definition:

    (define-IEEE-float IEEE-SANE-extended-float 
         :sign-bits 1 
         :exponent-bits 15 
         :mantissa-bits 64 
         :bias 16383 
         :associated-type 'long-float) 

Then in order to use the newly defined encoding, you can use the first example provided above to read and write (with type 'long-float), or use the newly created functions write-IEEE-SANE-extended-float and read-IEEE-SANE-extended-float:

    CL-USER> (with-open-file 
                  (s "test.bin" 
                     :direction :output 
                     :element-type '(unsigned-byte 8) 
                     :if-exists :supersede) 
     	       (define-endianess :little-endian) 
     	       (define-output-stream s) 
     	       (write-IEEE-SANE-extended-float 56.3332563322d0)) 
     NIL 
     CL-USER> (with-open-file  
                  (s "test.bin" 
                     :direction :input 
                     :element-type '(unsigned-byte 8)) 
     	       (define-endianess :little-endian) 
     	       (define-input-stream s) 
     	       (read-IEEE-SANE-extended-float)) 
     56.3332563322d0 
*endianess*    [Variable]

*endianess* has to be either :big-endian or :little-endian

define-endianess   endian  [Function]
define-input-stream   input  [Function]
define-output-stream   output  [Function]
string-code   s  [Function]

compute the ASCII-based numerical value of the string [warning: works only if the chars are coded in ASCII]

read-unsigned-integer   nb-bytes  [Function]

read an unsigned integer of nb-bytes bytes from *binary-input-stream*

read-signed-integer   nb-bytes  [Function]

read a signed integer of nb-bytes bytes from an array

write-unsigned-integer   quantity nb-bytes  [Function]

write an unsigned integer of nb-bytes bytes to *binary-output-stream*

write-signed-integer   number nb-bytes  [Function]

write a signed integer number on nb-bytes to *binary-output-stream*

ieee-float-reader   type  [Function]

Retrieve the reader function for a given type.

ieee-float-writer   type  [Function]

Retrieve the writer function for a given type.

with-gensyms   syms &body body  [Macro]
define-ieee-float   name &key (sign-bits 1) (exponent-bits 8) (mantissa-bits 23) (bias 127) (associated-type 'float)  [Macro]

Creates a IEEE float type and the associated writer and reader.

These are placed in a hash-table.