From ... From: Erik Naggum Subject: Re: Help with easy checksum Date: 2000/02/14 Message-ID: <3159560088216201@naggum.no>#1/1 X-Deja-AN: 585944973 References: <38A81589.8230412C@lmco.com> mail-copies-to: never Content-Type: text/plain; charset=us-ascii X-Complaints-To: newsmaster@eunet.no X-Trace: oslo-nntp.eunet.no 950578544 15525 195.0.192.66 (15 Feb 2000 01:35:44 GMT) Organization: Naggum Software; +47 8800 8879 or +1 510 435 8604; fax: +47 2210 9077; http://www.naggum.no User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 Mime-Version: 1.0 NNTP-Posting-Date: 15 Feb 2000 01:35:44 GMT Newsgroups: comp.lang.lisp,comp.text.interleaf * Scott Sheffield | I have created a binary file using Interleaf LISP and I need to put a | checksum at the end of the file so that it can be loaded into the | hardware. The problem is though I have a good idea what a checksum is | and does I have no idea how to do this in LISP. Here is what I am told I | need to do: when you are told what to do in language-specific terms, there's a serious flaw in the thinking of the person telling you what to do. I'll try to correct that serious flaw by simplifying your requirement to this simple rule: when the contents of the file is regarded as a sequence of 32-bit integers, the value of the last 32-bit integer of the file is such that the sum of the 32-bit integers throughout the file is 0 modulo 2^32. this is fairly easy to hack together, but it depends on how you write to your stream. if you write 32-bit integers to the stream (which means it should be opened with :ELEMENT-TYPE '(SIGNED-BYTE 32) or UNSIGNED-BYTE ditto), just sum them up while you write each integer, then write (LDB (BYTE 32 0) (- )) to the same stream just before closing it. if you don't write 32-bit integers to the file, you're probably better off writing it in one pass and reading it as 32-bit integers in another pass, only to tack on the same 32-bit integer as describe above at the end. | 2) Write 32 bit chunks to the file, also accumulate the 32 bit chunks | into this 32 bit integer object. The accumulation is a add, ignoring | overflows (which is standard), then the final step is to take the | negative (2's complement) all the accumulations have been done. no, ignoring overflows is _not_ standard, it's a a hardware-oriented design decision local to particular languages. the standard addition function does not overflow to begin with. please don't confuse the internal hardware representation of a value with the value. however, whether you ignore overflow or extract a bit field from an integer is only a conceptual difference. C-based life forms think in terms of ignoring overflow because they don't know what it means not to. Common Lisp programmers don't ignore overflows, but talk about values _modulo_ some other value, instead. #:Erik