all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Emacs Lisp - Reading a sequence of bytes as one integer
@ 2010-03-13 16:56 Jeff Clough
  2010-03-13 17:15 ` Teemu Likonen
  2010-03-13 18:49 ` Eli Zaretskii
  0 siblings, 2 replies; 8+ messages in thread
From: Jeff Clough @ 2010-03-13 16:56 UTC (permalink / raw)
  To: help-gnu-emacs

Hey now,

I'm stumbling through a project with elisp (mostly as an excuse to use
it again) and I'm stuck on something.

I have a (mostly) binary file I want to inspect.  There are several
places in this file where I want to read a series of bytes (in this
case, three consecutive bytes) and treat those bytes as a single
integer.  I can grab them as a buffer-substring obviously, but I'm at
a loss for how to make that into the integer I need, nor can I find
anything obvious that would be easier.

This is probably something ridiculously simple and clearly explained
somewhere, but I'm already going cross-eyed and my Google is failing
me.

Thanks for any help you can give.

Jeff





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 16:56 Emacs Lisp - Reading a sequence of bytes as one integer Jeff Clough
@ 2010-03-13 17:15 ` Teemu Likonen
  2010-03-13 18:10   ` Jeff Clough
  2010-03-13 18:49 ` Eli Zaretskii
  1 sibling, 1 reply; 8+ messages in thread
From: Teemu Likonen @ 2010-03-13 17:15 UTC (permalink / raw)
  To: Jeff Clough; +Cc: help-gnu-emacs

* 2010-03-13 11:56 (-0500), Jeff Clough wrote:

> I can grab them as a buffer-substring obviously, but I'm at a loss for
> how to make that into the integer I need, nor can I find anything
> obvious that would be easier.

You can use the Lisp reader:

    (read "123") => 123

The argument for READ can also be a buffer or a marker, for example. See
its documentation for more info.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 17:15 ` Teemu Likonen
@ 2010-03-13 18:10   ` Jeff Clough
  2010-03-13 19:10     ` Jeff Clough
  2010-03-13 19:42     ` Teemu Likonen
  0 siblings, 2 replies; 8+ messages in thread
From: Jeff Clough @ 2010-03-13 18:10 UTC (permalink / raw)
  To: help-gnu-emacs

From: Teemu Likonen <tlikonen@iki.fi>
Date: Sat, 13 Mar 2010 19:15:49 +0200

> You can use the Lisp reader:
> 
>     (read "123") => 123
> 
> The argument for READ can also be a buffer or a marker, for example. See
> its documentation for more info.

This is not seeming to work, and I'm not seeing how to make it work.
I can't tell if this is just my not getting something, or if it isn't
the right tool for the job.  Just in case, here's an example of what
I'm needing...

At certain positions in the file (and I can already find them), I need
to read a sequence of three bytes as an integer.  If I do a "hexdump
-c" on the file, I can see that those three bytes look like this "\0
\0 \t", which would give me a decimal value of 9 if read as a single
integer.  If I grab this sequence from the file using buffer-substring
and pass it to read, I get an end-of-file error.

Is there some general way I can tell emacs "Take the three bytes
following point and make one integer out of them"?

Thanks!

Jeff




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 16:56 Emacs Lisp - Reading a sequence of bytes as one integer Jeff Clough
  2010-03-13 17:15 ` Teemu Likonen
@ 2010-03-13 18:49 ` Eli Zaretskii
  2010-03-13 19:12   ` Jeff Clough
  1 sibling, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2010-03-13 18:49 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Sat, 13 Mar 2010 11:56:17 -0500 (EST)
> From: Jeff Clough <jeff@chaosphere.com>
> 
> I have a (mostly) binary file I want to inspect.  There are several
> places in this file where I want to read a series of bytes (in this
> case, three consecutive bytes) and treat those bytes as a single
> integer.  I can grab them as a buffer-substring obviously, but I'm at
> a loss for how to make that into the integer I need, nor can I find
> anything obvious that would be easier.

Take a look at bindat.el (it's bundled with Emacs).




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 18:10   ` Jeff Clough
@ 2010-03-13 19:10     ` Jeff Clough
  2010-03-14  8:03       ` Andreas Politz
  2010-03-13 19:42     ` Teemu Likonen
  1 sibling, 1 reply; 8+ messages in thread
From: Jeff Clough @ 2010-03-13 19:10 UTC (permalink / raw)
  To: help-gnu-emacs

From: Jeff Clough <jeff@chaosphere.com>
Date: Sat, 13 Mar 2010 13:10:29 -0500 (EST)

> Is there some general way I can tell emacs "Take the three bytes
> following point and make one integer out of them"?

Well, I've managed to figure this out, although it's the "hard way".
I grab the data as a string, then bust out each byte into its own
character/integer and finally I do the math to convert it into an
appropriate value.  My quick and dirty mock-up...

(defun decode-int (a-string)
  (+ (* (string-to-char (substring a-string 0 1)) 256 256)
     (* (string-to-char (substring a-string 1 2)) 256)
     (string-to-char (substring a-string 2 3))))

Still looking for a better way, but I'll put together a nicer version
of the above later that will serve my particular purposes.

Jeff





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 18:49 ` Eli Zaretskii
@ 2010-03-13 19:12   ` Jeff Clough
  0 siblings, 0 replies; 8+ messages in thread
From: Jeff Clough @ 2010-03-13 19:12 UTC (permalink / raw)
  To: help-gnu-emacs

From: Eli Zaretskii <eliz@gnu.org>
Date: Sat, 13 Mar 2010 20:49:28 +0200

> Take a look at bindat.el (it's bundled with Emacs).

Wow!  There's a lot of interesting stuff in there.  Thanks!

Jeff





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 18:10   ` Jeff Clough
  2010-03-13 19:10     ` Jeff Clough
@ 2010-03-13 19:42     ` Teemu Likonen
  1 sibling, 0 replies; 8+ messages in thread
From: Teemu Likonen @ 2010-03-13 19:42 UTC (permalink / raw)
  To: Jeff Clough; +Cc: help-gnu-emacs

* 2010-03-13 13:10 (-0500), Jeff Clough wrote:

>>     (read "123") => 123

> This is not seeming to work, and I'm not seeing how to make it work. I
> can't tell if this is just my not getting something, or if it isn't
> the right tool for the job.

Never mind, I misunderstood your original question and solved a
different problem. :-)




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Emacs Lisp - Reading a sequence of bytes as one integer
  2010-03-13 19:10     ` Jeff Clough
@ 2010-03-14  8:03       ` Andreas Politz
  0 siblings, 0 replies; 8+ messages in thread
From: Andreas Politz @ 2010-03-14  8:03 UTC (permalink / raw)
  To: help-gnu-emacs

Jeff Clough <jeff@chaosphere.com> writes:

> From: Jeff Clough <jeff@chaosphere.com>
> Date: Sat, 13 Mar 2010 13:10:29 -0500 (EST)
>
>> Is there some general way I can tell emacs "Take the three bytes
>> following point and make one integer out of them"?
>
> Well, I've managed to figure this out, although it's the "hard way".
> I grab the data as a string, then bust out each byte into its own
> character/integer and finally I do the math to convert it into an
> appropriate value.  My quick and dirty mock-up...
>
> (defun decode-int (a-string)
>   (+ (* (string-to-char (substring a-string 0 1)) 256 256)
>      (* (string-to-char (substring a-string 1 2)) 256)
>      (string-to-char (substring a-string 2 3))))
>
> Still looking for a better way, but I'll put together a nicer version
> of the above later that will serve my particular purposes.
>
> Jeff

string-to-list/get-byte , logior and lsh might come in handy.

-ap





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-03-14  8:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-13 16:56 Emacs Lisp - Reading a sequence of bytes as one integer Jeff Clough
2010-03-13 17:15 ` Teemu Likonen
2010-03-13 18:10   ` Jeff Clough
2010-03-13 19:10     ` Jeff Clough
2010-03-14  8:03       ` Andreas Politz
2010-03-13 19:42     ` Teemu Likonen
2010-03-13 18:49 ` Eli Zaretskii
2010-03-13 19:12   ` Jeff Clough

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.