Subject: Re: Q: How to write binary data to a file?
From: Erik Naggum <erik@naggum.no>
Date: 03 Sep 2002 20:31:42 +0000
Newsgroups: comp.lang.lisp
Message-ID: <3240073902064268@naggum.no>

* Ryan M. Rifkin
| I'm a (relative) Lisp newbie, trying to learn.  I'm interested in writing
| integers to a file as 16-bit signed integers.  (The integers I need to write
| will fit in this representation.  I am writing audio data to the file, which
| is why I need to output in a binary format).

  Just specify `:element-type ' (signed-byte 16)´ and go ahead and `write-byte´
  values in the appropriate range to the stream.

| I'm aware of the function write-byte, but I'm not sure how to write my
| signed integers.

  `write-byte´ does not write IBMese 8-bit bytes, but real bytes, contiguous
  sequences of bits in an integer.  In the words of the Common Lisp Glossary:

byte n.
1. adjacent bits within an integer.  (The specific number of bits can vary
from point to point in the program; see the function `byte´.)
2. an integer in a specified range.  (The specific range can vary from point
to point in the program; see the functions `open´ and `write-byte´.)

  This definition of "byte" predates the IBM abuse of the term and comes from
  the PDP-6 and -10 hardware designs, which were 36-bit word machines, meaning
  the smallest addressable unit was one machine word of 36 bits.  I believe IBM
  invented the byte-addressable hardware.

  Your main confusion comes from thinking that "byte" means the same thing in
  Common Lisp as it means in, say, C.

-- 
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.