Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Reading Binary Files: Best Practices?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Bob Felts  
View profile  
 More options Dec 14 2008, 12:38 am
Newsgroups: comp.lang.lisp
From: w...@stablecross.com (Bob Felts)
Date: Sun, 14 Dec 2008 00:38:13 -0500
Local: Sun, Dec 14 2008 12:38 am
Subject: Reading Binary Files: Best Practices?
Seibel's book, PCL, has a section on parsing binary files.  He gives an
example of reading an unsigned 16-bit thing:

   (defun read-ui16 (stream)
     (let ((ui16 0))
     (setf (ldb (byte 8 8) ui16) (read-byte stream))
     (setf (ldb (byte 8 0) ui16) (read-byte stream))
     ui16))

So far, so good.  How would a Lisp expert read a signed 16-bit thing?

One might, for example, write (sign-ui16 (read-ui16 stream)), where
sign-ui16 is defined as follows:

   (defun sign-ui16 (n)
     (if (zerop (ldb (byte 1 15) n))
       n
       (- (1+ (logxor #xffff n)))))

If the sign-bit is zero, return the number.  If it's set, return -(~n +
1).  (Note: lognot doesn't work - it has to be logxor).

Any suggestions on a better way to do this?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Madhu  
View profile  
 More options Dec 14 2008, 1:12 am
Newsgroups: comp.lang.lisp
From: Madhu <enom...@meer.net>
Date: Sun, 14 Dec 2008 11:42:42 +0530
Local: Sun, Dec 14 2008 1:12 am
Subject: Re: Reading Binary Files: Best Practices?

* (Bob Felts) <1irwxg6.14vwqbobbbm70N%w...@stablecross.com> :
Wrote on Sun, 14 Dec 2008 00:38:13 -0500:

|    (defun sign-ui16 (n)
|      (if (zerop (ldb (byte 1 15) n))
|        n
|        (- (1+ (logxor #xffff n)))))
|
| If the sign-bit is zero, return the number.  If it's set, return -(~n +
| 1).  (Note: lognot doesn't work - it has to be logxor).
|
| Any suggestions on a better way to do this?

Here is general code from Pascal Bourguignon

  (defun unsigned-to-signed/2-complement (x width)
    (declare (integer x width))
    (let ((maxpos+1 (expt 2 (1- width))))
         (if (< x maxpos+1) x (- x (* 2 maxpos+1)))))

Most of the code I've seen which reads signed 16bit words is along these
lines: declare the variable to be an unsigned-byte 16, check and
subtract 65536, or just return n.

This does not address how n was read from the external file, endianness
in which the file was stored.  For parsing binary files I've been using
frodef's excellent

        <URL:http://www.cs.uit.no/~frodef/sw/binary-types/>  

(from even before PCL came out.) There you'd do

(read-binary 's16 stream)

--
Madhu


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zach Beane  
View profile  
 More options Dec 14 2008, 9:35 am
Newsgroups: comp.lang.lisp
From: Zach Beane <x...@xach.com>
Date: Sun, 14 Dec 2008 09:35:52 -0500
Local: Sun, Dec 14 2008 9:35 am
Subject: Re: Reading Binary Files: Best Practices?

w...@stablecross.com (Bob Felts) writes:
> So far, so good.  How would a Lisp expert read a signed 16-bit thing?

> One might, for example, write (sign-ui16 (read-ui16 stream)), where
> sign-ui16 is defined as follows:

>    (defun sign-ui16 (n)
>      (if (zerop (ldb (byte 1 15) n))
>        n
>        (- (1+ (logxor #xffff n)))))

> If the sign-bit is zero, return the number.  If it's set, return -(~n +
> 1).  (Note: lognot doesn't work - it has to be logxor).

> Any suggestions on a better way to do this?

Christophe Rhodes showed me this general idea:

  (defun sign-sized (n size)
    (if (logbitp (1- size) n)
        (dpb n (byte size 0) -1)
        n))

So:

  (defun sign-ui16 (n)
    (sign-sized n 16))

Zach


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Felts  
View profile  
 More options Dec 14 2008, 3:06 pm
Newsgroups: comp.lang.lisp
From: w...@stablecross.com (Bob Felts)
Date: Sun, 14 Dec 2008 15:06:32 -0500
Local: Sun, Dec 14 2008 3:06 pm
Subject: Re: Reading Binary Files: Best Practices?

Thanks.   Excellent information.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google