I'd really like to be able to write out 32 bit sized numbers to a file, in
binary. While reading through CL:TL2, it seems that the only binary IO is
writing a single byte and reading a single byte. And while this isn't bad
for reading or writing strings of characters, for storing a fixed size
number it kinda sucks. Anyway, if anyone can give me a pointer or two on
how to properly write out a 4 8-bit byte number to a stream, I'd
appreciate it. And I'd even appreciate it more if you also had advice on
how to read it back in :-)
And in the interest of disclosure, this is for a homework assignment. I'm
writing a B-Tree for a file structures class. The assignment was
originally in Java, but my professor said he didn't have a problem with me
doing it in lisp. So I figured I might as well get some more practice.
Andy Cristina <········@cs.uno.edu> wrote:
> I'd really like to be able to write out 32 bit sized numbers to a file, in
> binary. While reading through CL:TL2, it seems that the only binary IO is
> writing a single byte and reading a single byte. And while this isn't bad
Try looking in the hyperspec instead... OPEN accepts :element-type, which
does what you want.
CL-USER(1): (defvar *byte* (1- (expt 2 32)))
*BYTE*
CL-USER(2): *byte*
4294967295
CL-USER(3): (with-open-file (f "/tmp/32bit" :direction :output
:element-type '(unsigned-byte 32))
(write-byte *byte* f))
4294967295
CL-USER(4): (with-open-file (f "/tmp/32bit" :element-type '(unsigned-byte 32))
(read-byte f))
4294967295
CL-USER(5):
Cheers,
-- Nikodemus
<·········@random-state.net> wrote:
+---------------
| Andy Cristina <········@cs.uno.edu> wrote:
| > I'd really like to be able to write out 32 bit sized numbers to a file, in
| > binary. While reading through CL:TL2, it seems that the only binary IO is
| > writing a single byte and reading a single byte. And while this isn't bad
|
| Try looking in the hyperspec instead... OPEN accepts :element-type, which
| does what you want.
+---------------
Additionally, look at the HyperSpec entries for READ-SEQUENCE and
WRITE-SEQUENCE, especially the notes at the bottom:
An efficient implementation is more likely to exist for the case
where the sequence is a vector with the same element type as the
stream.
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
····@rpw3.org (Rob Warnock) writes:
> <·········@random-state.net> wrote:
> | Andy Cristina <········@cs.uno.edu> wrote:
> | > I'd really like to be able to write out 32 bit sized numbers to
> | > a file, in binary. While reading through CL:TL2, it seems that
> | > the only binary IO is writing a single byte and reading a single
> | > byte.
> | Try looking in the hyperspec instead... OPEN accepts :element-type, which
> | does what you want.
> Additionally, look at the HyperSpec entries for READ-SEQUENCE and
> WRITE-SEQUENCE
Also, Binary-types may be of interest:
http://www.cliki.net/Binary-types
--
Lars Brinkhoff, Services for Unix, Linux, GCC, HTTP
Brinkhoff Consulting http://www.brinkhoff.se/