help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Inspecting integer, float values from hexl-mode


From: Alberto Luaces
Subject: Re: Inspecting integer, float values from hexl-mode
Date: Fri, 11 Feb 2011 17:02:37 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Stefan Monnier writes:

>>>>>> "Alberto" == Alberto Luaces writes:
>
>> Hello,
>> I'm inspecting a binary file with hexl-mode. I need to select some bytes
>> and then be able to interpret their value as integer, or as float. I was
>> wondering if there is a method for doing this since it seems that
>> hexl-mode doesn't do that by default.
>
> Not that I know, no.
>
>> In case there isn't one, I suppose I would have to code my method
>> calling the external `od' program to interpret the values.
>
> That sounds like a reasonably straightforward way to do it, yes.
>
>> The problem here would be how to compute the offset for the binary
>> file that the selected region in emacs is representing.
>
> The function hexl-current-address should do just that.
>
> It would probably make sense to try and include such a feature in
> hexl.el, so I encourage you to post your code for inclusion once you
> have it working (and send it along for help if you have trouble getting
> it to work, of course).

I have gone the route of using `od' since it simplifies a lot the
interpretation of floating point types. So far I have written a small
function that reads floats, but it's trivial to modify in order to show
doubles, integers, chars... just by passing the arguments to "-t" and
"-N" as an argument:

Comments on style and correctness are welcome.

--8<---------------cut here---------------start------------->8---

(defun hexl-interpret-float-at-point ()
  (interactive)
  
  (let ((out-buffer "*hexloutput*"))

    ;; Calls od

    (call-process
     "od"
     (buffer-file-name)
     out-buffer
     nil
     "-t"
     "f4"
     "-j"
     (number-to-string (hexl-current-address))
     "-N4"
     )

    ;; Reads the number from the output buffer

    (save-excursion 
      (set-buffer out-buffer)
      (goto-char (point-min))
      (re-search-forward "[^[:space:]]+[[:space:]]+\\([^[:space:]]+\\)")
      (message (format "%s" (match-string 1)))
      )

    (kill-buffer out-buffer)
    )
  )

--8<---------------cut here---------------end--------------->8---

-- 
Alberto




reply via email to

[Prev in Thread] Current Thread [Next in Thread]