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).
I'm aware of the function write-byte, but I'm not sure how to write my
signed integers. I could try to implement it on top of write-byte,
but this seems messy. Is there any reasonable way to do this?
Alternately, I could just output a list of integers, and then write a
six-line perl script to convert, but this seems inelegant.
For the record, I'm using CMUCL.
Cheers,
rif
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.
I won't swear on portability and conformance though.
===========================================================================
* (defvar *v* (make-array 5 :element-type '(unsigned-byte 16)
:initial-contents '( 1 2 3 4 5 )))
*V*
* (with-open-file (z "home:tmp/zut.bin"
:element-type '(unsigned-byte 16)
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(write-sequence *v* z))
#(1 2 3 4 5)
* (defvar *vv* (make-array 5 :element-type '(unsigned-byte 16)))
*VV*
* *vv*
#(0 0 0 0 0)
* (with-open-file (z "home:tmp/zut.bin"
:element-type '(unsigned-byte 16)
:direction :input
:if-does-not-exist :error)
(read-sequence *vv* z))
5
* *vv*
#(1 2 3 4 5)
===========================================================================
You will have to look at "home:tmp/zut.bin" in hexl-mode.
Cheers
--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group tel. +1 - 212 - 998 3488
715 Broadway 10th Floor fax +1 - 212 - 995 4122
New York, NY 10003, USA http://bioinformatics.cat.nyu.edu
"Hello New York! We'll do what we can!"
Bill Murray in `Ghostbusters'.
> [..] I'm aware of the function write-byte, but I'm not sure how to
> write my signed integers. I could try to implement it on top of
> write-byte, but this seems messy. Is there any reasonable way to do
> this?
It's not that messy. Just make sure you have a stream with element-type
of 8-bit bytes, which is typically achieved with opening the file with
:element-type '(unsigned-byte 8), and then do something like
(defun write-16-bits-big-endian (value stream)
(check-type value (signed-byte 16))
(write-byte (ldb (byte 8 8) value))
(write-byte (ldb (byte 8 0) value))
value)
If you care about performance, CMUCL might or might not support
writing buffers of such bytes directly. Stuff your values into vectors
with that element-type:
(make-array <buffer-size> :element-type '(signed-byte 16) ..)
Have your stream use the same element-type, and use write-sequence,
something like this:
(with-open-file (stream <path> :element-type '(signed-byte 16) ..)
(write-sequence <buffer> stream))
The latter strategy might yield similar performance to the C
equivalent, but depends to a larger extent on the implementation, in
your case CMUCL (which now also determines representational
characteristics like endianess), while the former strategy only
requires the implementation to provide 8-bit binary streams.
From a cursory look at CMUCL (I don't use it much), it seems to
support the write-sequence strategy, because:
* (upgraded-array-element-type '(signed-byte 16))
=> (SIGNED-BYTE 16)
* (with-open-file (stream #p"/dev/null" :element-type '(signed-byte 16))
(stream-element-type stream))
=> (SIGNED-BYTE 16)
This is no guarantee that what you expect actually ends up in the
file, but I believe it's a good indication that CMUCL will do the
"right" thing.
You might also find my binary-types package interesting:
<URL:http://www.cs.uit.no/~frodef/sw/binary-types/>
> Alternately, I could just output a list of integers, and then write
> a six-line perl script to convert, but this seems inelegant.
Please don't... :)
--
Frode Vatvedt Fjeld
In most languages, bytes are defined to be of a fixed size. So usually,
a byte occupies 8 bits. However, Common Lisp allows you to define your
bytes to be of arbitrary size, including 16 bits.
You can define the base type of a file when opening the file. So for example
(with-open-file (stream pathname :element-type '(signed-byte 16)
...)
lets you operate on a file that containes signed 16-bit values. Here,
read-byte and write-byte do what you actually want without any further
ado. (Cool, eh? ;) )
(This example is not complete, you also need to specify that you want to
write to the particular file, and not just read it. See the
specifications for open-file and with-open-file. Also, handling of
pathnames is different from other languages and you need to consult the
documentation of your Common Lisp implementation for creating correct
pathnames.)
Pascal
> Ryan M. Rifkin wrote:
> > Hi.
> > 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).
> > I'm aware of the function write-byte, but I'm not sure how to write
> > my
>
> > signed integers.
>
> In most languages, bytes are defined to be of a fixed size. So
> usually, a byte occupies 8 bits. However, Common Lisp allows you to
> define your bytes to be of arbitrary size, including 16 bits.
I don't know too many languages that actually define the term "byte".
However, I'll not argue with the accuracy in modern times of your
statement, especially in light of the almost overwhelming popular usage
of the term "byte" to in fact mean 8 bits. However, it is historically
inaccurate, and without understanding the history one loses the reasoning
behind the choice of CL in choosing such a definition for byte. I
always try to use "octet" instead, when referring specifically to
8-bit bytes.
See
Unfortunately, the link contained within that article no longer
works, but try instead
http://www.wkonline.com/d/byte.html for similar explanations, or
http://www.bobbemer.com/BYTE.HTM (also, the same article is at
http://serendipity.nofadz.com/hermetic/cfunlib/bemer1.htm
or http://hermetic.magnet.ch/cfunlib/bemer1.htm)
--
Duane Rettig du...@franz.com Franz Inc. http://www.franz.com/
555 12th St., Suite 1450 http://www.555citycenter.com/
Oakland, Ca. 94607 Phone: (510) 452-2000; Fax: (510) 452-0182
>>In most languages, bytes are defined to be of a fixed size. So
>>usually, a byte occupies 8 bits. However, Common Lisp allows you to
>>define your bytes to be of arbitrary size, including 16 bits.
>
>
> I don't know too many languages that actually define the term "byte".
> However, I'll not argue with the accuracy in modern times of your
> statement, especially in light of the almost overwhelming popular usage
> of the term "byte" to in fact mean 8 bits. However, it is historically
> inaccurate,
[...]
Wow, I wasn't aware of that. Thanks for the links - very interesting!
Pascal
> I don't know too many languages that actually define the term "byte".
> However, I'll not argue with the accuracy in modern times of your
> statement, especially in light of the almost overwhelming popular usage
> of the term "byte" to in fact mean 8 bits.
I'm starting to think that the the most valuable CS education I ever got was my
exposure to the university DEC-10 computer. Which used several byte sizes (for
instance, a 5-bit character set), which had shared memory, a myriad of extraordinary
computer language implementations (e.g. the famous Edinburgh Prolog), and so on :-)
--
(espen)
> However, I'll not argue with the accuracy in modern times of your
> statement, especially in light of the almost overwhelming popular usage
> of the term "byte" to in fact mean 8 bits. However, it is historically
> inaccurate, and without understanding the history one loses the reasoning
> behind the choice of CL in choosing such a definition for byte. I
> always try to use "octet" instead, when referring specifically to
> 8-bit bytes.
Givin' in to the rude French, aren't we? :)
> Duane Rettig <du...@franz.com> writes:
>
> > However, I'll not argue with the accuracy in modern times of your
> > statement, especially in light of the almost overwhelming popular usage
> > of the term "byte" to in fact mean 8 bits. However, it is historically
> > inaccurate, and without understanding the history one loses the reasoning
> > behind the choice of CL in choosing such a definition for byte. I
> > always try to use "octet" instead, when referring specifically to
> > 8-bit bytes.
>
> Givin' in to the rude French, aren't we? :)
Sorry? I don't understand the joke, here.
I'm not sure what the joke is, either, but it might have something to
do with the French use of new French words for some common programming
terms. They refer to octets and megaoctets instead of bytes and
megabytes. And instead of software they write "logiciels". A recent
conference discussed "logiciels libres", for example.
Zach
--
Zachary Beane xa...@xach.com
> In article <4ofbdw...@beta.franz.com>, Duane Rettig wrote:
> > Marco Antoniotti <mar...@cs.nyu.edu> writes:
> >
> >> Duane Rettig <du...@franz.com> writes:
> >>
...
> >> > always try to use "octet" instead, when referring specifically to
> >> > 8-bit bytes.
> >>
> >> Givin' in to the rude French, aren't we? :)
> >
> > Sorry? I don't understand the joke, here.
>
> I'm not sure what the joke is, either, but it might have something to
> do with the French use of new French words for some common programming
> terms. They refer to octets and megaoctets instead of bytes and
> megabytes. And instead of software they write "logiciels". A recent
> conference discussed "logiciels libres", for example.
>
Pretty much that :) ... and also the tendency in (most of) the US
media to spurn everything French :)
I had to give in to the "rude Italians," last night at dinner, so I
don't think the French have any kind of monopoly on this sort of
thing...
--
(reverse (concatenate 'string "gro.gultn@" "enworbbc"))
http://cbbrowne.com/info/sap.html
Rules of the Evil Overlord #106. "If my supreme command center comes
under attack, I will immediately flee to safety in my prepared escape
pod and direct the defenses from there. I will not wait until the
troops break into my inner sanctum to attempt this."
<http://www.eviloverlord.com/>
> xa...@xach.com (Zachary Beane) writes:
>
> > In article <4ofbdw...@beta.franz.com>, Duane Rettig wrote:
> > > Marco Antoniotti <mar...@cs.nyu.edu> writes:
> > >
> > >> Duane Rettig <du...@franz.com> writes:
> > >>
> ...
> > >> > always try to use "octet" instead, when referring specifically to
> > >> > 8-bit bytes.
> > >>
> > >> Givin' in to the rude French, aren't we? :)
> > >
> > > Sorry? I don't understand the joke, here.
> >
> > I'm not sure what the joke is, either, but it might have something to
> > do with the French use of new French words for some common programming
> > terms. They refer to octets and megaoctets instead of bytes and
> > megabytes. And instead of software they write "logiciels". A recent
> > conference discussed "logiciels libres", for example.
> >
>
> Pretty much that :) ... and also the tendency in (most of) the US
> media to spurn everything French :)
You mean "octet"? Marco, I'm suprised at you. My dictionary derives
octet as an Italian term, "oct" meaning 8 from the Latin, and
the "et ending is a derivation of the musical term "duet".
And spurning Frech? At least a third of the English language is
derived from French (but just not "octet" :-) If the US media
truly spurned everything French or derived from French, they would
produce copy that would sound extremely strange to a native speaker.
> Oops! Marco Antoniotti <mar...@cs.nyu.edu> was seen spray-painting on a wall:
> > Duane Rettig <du...@franz.com> writes:
> >
> >> However, I'll not argue with the accuracy in modern times of your
> >> statement, especially in light of the almost overwhelming popular usage
> >> of the term "byte" to in fact mean 8 bits. However, it is historically
> >> inaccurate, and without understanding the history one loses the reasoning
> >> behind the choice of CL in choosing such a definition for byte. I
> >> always try to use "octet" instead, when referring specifically to
> >> 8-bit bytes.
> >
> > Givin' in to the rude French, aren't we? :)
>
> I had to give in to the "rude Italians," last night at dinner, so I
> don't think the French have any kind of monopoly on this sort of
> thing...
We, rude!?! How can it be? We don't take ourselves seriously enough
to be rude. :)
> Marco Antoniotti <mar...@cs.nyu.edu> writes:
>
> > xa...@xach.com (Zachary Beane) writes:
> >
> > > In article <4ofbdw...@beta.franz.com>, Duane Rettig wrote:
> > > > Marco Antoniotti <mar...@cs.nyu.edu> writes:
> > > >
> > > >> Duane Rettig <du...@franz.com> writes:
> > > >>
> > ...
> > > >> > always try to use "octet" instead, when referring specifically to
> > > >> > 8-bit bytes.
> > > >>
> > > >> Givin' in to the rude French, aren't we? :)
> > > >
> > > > Sorry? I don't understand the joke, here.
> > >
> > > I'm not sure what the joke is, either, but it might have something to
> > > do with the French use of new French words for some common programming
> > > terms. They refer to octets and megaoctets instead of bytes and
> > > megabytes. And instead of software they write "logiciels". A recent
> > > conference discussed "logiciels libres", for example.
> > >
> >
> > Pretty much that :) ... and also the tendency in (most of) the US
> > media to spurn everything French :)
>
> You mean "octet"? Marco, I'm suprised at you. My dictionary derives
> octet as an Italian term, "oct" meaning 8 from the Latin, and
> the "et ending is a derivation of the musical term "duet".
Well, almost. The correct Italian is "ottetto". As for "duet" it is
still not Italian: the correct form is "duetto". "octet" is a (AFAIK)
correct French. Of course it is a Latinism.
I'll pass on the rest. :)
Perhaps, but my English dictionary makes no mention of French in either
of the etymolies for octet or duet. But in fact, it does
specifically mention Italian for the etymology for duet which
agrees with you on everything but the mention of French:
"[< Italian duetto (diminuitive) < duo; see etym. under duo]"
and under duo:
"[< Italian duo < Latin duo = two]"
No mention of French. Hence my original density to the joke.
> I'll pass on the rest. :)
Same here; how long can we beat a dead joke?
> I'm not sure what the joke is, either, but it might have something to
> do with the French use of new French words for some common programming
> terms. They refer to octets and megaoctets instead of bytes and
> megabytes. And instead of software they write "logiciels". A recent
> conference discussed "logiciels libres", for example.
This is really cool. Not only have they decided to use a weird term,
*they've got it wrong* because - as any Lisp person should know,
especially one who has read this thread - a byte *isn't* an octet, or
not in all uses of the term! So some time in some French article
(perhaps on Common Lisp), someone is going to correct `byte' to
`octet' and completely destroy the meaning of what was written
--tim
A Dictionary of Computing from Oxford University Press actually has this
informative entry:
octet Eight contiguous bits; an eight bit byte. The term is used instead of
byte to prevent confusion in cases where the term has preexisting hardware
associations, as in machines with 7-bit bytes, 9-bit bytes, 12-bit bytes.
But their "byte" idea is "a fixed number of bits that can be treated as a
unit by the computer hardware", which is just plain wrong.
> Marco Antoniotti <mar...@cs.nyu.edu> writes:
>
> > Well, almost. The correct Italian is "ottetto". As for "duet" it is
> > still not Italian: the correct form is "duetto". "octet" is a (AFAIK)
> > correct French. Of course it is a Latinism.
>
> Perhaps, but my English dictionary makes no mention of French in either
> of the etymolies for octet or duet. But in fact, it does
> specifically mention Italian for the etymology for duet which
> agrees with you on everything but the mention of French:
>
> "[< Italian duetto (diminuitive) < duo; see etym. under duo]"
>
> and under duo:
>
> "[< Italian duo < Latin duo = two]"
>
> No mention of French. Hence my original density to the joke.
Well, I do not know how your dictionary was compiled and I am no
etymologist; the information contained in ther may be right after all.
All I can say is that there is a "rule" in Italian for words denoting
N-tuples up to a certain small number: duetto, terzetto, quartetto,
quintetto, sestetto, settetto, ottetto.
I don't remember my Latin, but I am sure that you may find the roots
in there.
> > I'll pass on the rest. :)
>
> Same here; how long can we beat a dead joke?
Not too long :)
As an aside, have you noticed how the best comedians just know when to quit?
Renzo Arbore in Italy and Jerry Seinfeld in the US come to mind.
Ok, ok, enough of this. Back to read the "ilias".
Duane Rettig wrote:
> Perhaps, but my English dictionary makes no mention of French in either
> of the etymolies for octet or duet.
Mine neither, but there is a tantalizing specification of "octette" as
an alternative to octet. The etymology is the Italian otetto.
My LaRousse translates english byte to french octet, but has no mention
of octette.
Go figger.
kenny
you can of course alias the name 'write-byte' to the precise name
'write-element'.
(defmacro write-byte (&rest args)
`( write-element ,@args))
just another version.
This reminds me of the old Steve Martin line:
"Those French, they have a different word for everything!"
(How dare they?)
>And spurning Frech? At least a third of the English language is
>derived from French (but just not "octet" :-) If the US media
>truly spurned everything French or derived from French, they would
>produce copy that would sound extremely strange to a native speaker.
>
I think it is even quite a bit higher (in terms of number of words). Most
words of latin origin actually came from French, and latin origin words
certainly make up the majority of English words.
> Perhaps, but my English dictionary makes no mention of French in either
> of the etymolies for octet or duet.
Well, if we're getting out the dictionaries ... *heft* ... my Oxford
says that the English "octet" comes from the Italian "ottetto", or
from the Greek "Oktett". However, the closest it comes to the 8-bit
byte meaning of the word, is "a group of eight."
My Petit Robert, on the other hand, defines the French "octet" as "a
base composed of eight binary digits, used in most machine languages",
and claims it was derived in 1960 from the root "oct-", with no
mention of Italian, or eight-person ensembles. Pleasantly enough, it
defines "byte" to mean "a collection of some bits constituting a
complete unit of information", and says it is sometimes used
incorrectly to mean "byte".
So I'd guess that the English meaning for octet of "8-bit byte" comes
from the French, but since English had previously derived the word
"octet" from Italian for other uses, you won't find mention of French
in its etymology in most dictionaries.
> how long can we beat a dead joke?
Is this a challenge?
--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'
dr> almost overwhelming popular usage of the term "byte" to in fact
dr> mean 8 bits. However, it is historically inaccurate, and without
dr> understanding the history one loses the reasoning behind the
dr> choice of CL in choosing such a definition for byte. I always
dr> try to use "octet" instead, when referring specifically to 8-bit
dr> bytes.
yes, I do likewise, but have had referees of scientific articles
"correct" me on this.
>>>>> "tb" == Tim Bradshaw <t...@cley.com> writes:
tb> Not only have [the French] decided to use a weird term, *they've
tb> got it wrong* because - as any Lisp person should know,
tb> especially one who has read this thread - a byte *isn't* an
tb> octet, or not in all uses of the term!
most dictionaries have a good definition, and use «multiplet» for byte
(and eg «triplet» for a 3-bit byte). The government unfortunately got
it wrong in their official translations for technical terms, published
in the Journal Officiel.
<URL:http://www.culture.fr/culture/dglf/cogeter/10-10-98-3.htm>
>>>>> "rc" == Roger Corman <ro...@corman.net> writes:
rc> This reminds me of the old Steve Martin line:
rc> "Those French, they have a different word for everything!"
c'est «tout», that's all folks!
--
Eric Marsden <URL:http://www.laas.fr/~emarsden/>
sorry the opposite of course:
(defmacro write-element (&rest args)
`( write-byte ,@args))
and 'precise' is of course a relative.
> >>>>> "dr" == Duane Rettig <du...@franz.com> writes:
>
> dr> almost overwhelming popular usage of the term "byte" to in fact
> dr> mean 8 bits. However, it is historically inaccurate, and without
> dr> understanding the history one loses the reasoning behind the
> dr> choice of CL in choosing such a definition for byte. I always
> dr> try to use "octet" instead, when referring specifically to 8-bit
> dr> bytes.
>
> yes, I do likewise, but have had referees of scientific articles
> "correct" me on this.
See below.
> >>>>> "tb" == Tim Bradshaw <t...@cley.com> writes:
>
> tb> Not only have [the French] decided to use a weird term, *they've
> tb> got it wrong* because - as any Lisp person should know,
> tb> especially one who has read this thread - a byte *isn't* an
> tb> octet, or not in all uses of the term!
>
> most dictionaries have a good definition, and use «multiplet» for byte
> (and eg «triplet» for a 3-bit byte). The government unfortunately got
> it wrong in their official translations for technical terms, published
> in the Journal Officiel.
>
> <URL:http://www.culture.fr/culture/dglf/cogeter/10-10-98-3.htm>
Wow - This proves that we English speakers couldn't have derived our
definition of octet from the French, because in French, "octet"
means "byte"! :-)
Back to what you said earlier:
> yes, I do likewise, but have had referees of scientific articles
> "correct" me on this.
But if your articles were in French, and the referees were French, then
they were right to correct you ("When in Rome ..." Oops, I mean ... )
:-)
Yeah, me too. I've told the story several (too many?) times:
<URL:http://groups.google.com/groups?q=rpw3+focal+sortj+macros&hl=en&
lr=&ie=UTF-8&selm=938v11%24m89cq%241%40fido.engr.sgi.com&rnum=2>
<URL:http://groups.google.com/groups?q=rpw3+focal+sortj+macros&hl=en&
lr=&ie=UTF-8&selm=7meuue%2498dks%40fido.engr.sgi.com&rnum=1>
<URL:http://groups.google.com/groups?q=rpw3+bit+strips&hl=en&lr=&
ie=UTF-8&selm=3232%40fortune.UUCP&rnum=1>
so I won't repeat it here, but the byte pointer hardware on the PDP-10
got used for a *lot* more stuff than just accessing character strings...
-Rob
-----
Rob Warnock, PP-ASEL-IA <rp...@rpw3.org>
627 26th Avenue <URL:http://www.rpw3.org/>
San Mateo, CA 94403 (650)572-2607
Actually, good dictionaries (e.g., the O.E.D) distinguish between
words derived from french, old french, and Latin.
Since a very small percentage of the english speaking population was
literate for most of the history of the language, and as Latin was the
language of learning, most literate people were directly familiar with
Latin for much of the history of english. In addition, those who would
take it upon themselves to coin a new word (or borrow one) and
_publish_ it (after all, we only have records of those neologisms that
were published) were even more likely to be well educated, and hence,
versed in latin.
So, english acquired many words directly from french (because of the
Norman conquest - the Normans (i.e., Norsemen) spoke french by the
time they invaded England), but also took many directly from latin, as
most educated people read and wrote latin until a century or so ago.
> Most words of
1, 2 & 3 germanic
>latin origin actually
1, 2 & 3 latinate
>came from French, and
4, 5, 6, & 7 germanic
>latin origin
4 & 5 latinate
> words
8 germanic
> certainly
6 latinate
> make up the
9, 10, & 11 germanic
> majority
7 latinate
> of English words.
12, 13, & 14 germanic.
Not in your sentence, which has more words of germanic origin than
latin/french by two to one. ;)