* Inspecting integer, float values from hexl-mode
@ 2011-02-03 9:20 Alberto Luaces
0 siblings, 0 replies; 4+ messages in thread
From: Alberto Luaces @ 2011-02-03 9:20 UTC (permalink / raw)
To: help-gnu-emacs
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.
In case there isn't one, I suppose I would have to code my method
calling the external `od' program to interpret the values. The problem
here would be how to compute the offset for the binary file that the
selected region in emacs is representing.
Thanks,
--
Alberto
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Inspecting integer, float values from hexl-mode
[not found] <mailman.1.1296724832.13967.help-gnu-emacs@gnu.org>
@ 2011-02-04 15:25 ` Stefan Monnier
2011-02-11 16:02 ` Alberto Luaces
2011-02-04 15:38 ` despen
1 sibling, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2011-02-04 15:25 UTC (permalink / raw)
To: help-gnu-emacs
>>>>> "Alberto" == Alberto Luaces <aluaces@udc.es> 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).
Stefan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Inspecting integer, float values from hexl-mode
[not found] <mailman.1.1296724832.13967.help-gnu-emacs@gnu.org>
2011-02-04 15:25 ` Inspecting integer, float values from hexl-mode Stefan Monnier
@ 2011-02-04 15:38 ` despen
1 sibling, 0 replies; 4+ messages in thread
From: despen @ 2011-02-04 15:38 UTC (permalink / raw)
To: help-gnu-emacs
Alberto Luaces <aluaces@udc.es> 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.
hexl-mode shows integers in byte order.
On Intel platforms that makes mental conversion from hex to integer
just a little harder.
I use this to convert hex in logical byte order to decimal:
(defun x2c (num &optional arg)
"convert hex arg or word at point to decimal."
(interactive
(list
(let* ((num-chars "0-9A-Fa-f")
(ins-at (point))
(default-hex (save-excursion
(buffer-substring
(progn
(re-search-backward "\\sw" nil t) ;backup a word
(skip-chars-backward num-chars) (point)) ;back up hex chr
(progn (skip-chars-forward num-chars) (point)))))
(override-hex (read-string
(if (equal default-hex "") "Hex Number: "
(concat "Hex Number: (default " default-hex ") ")))))
(if (equal override-hex "") default-hex override-hex))
(prefix-numeric-value current-prefix-arg)))
(insert (format " = %s" (int-to-string (string-to-int num 16)))))
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Inspecting integer, float values from hexl-mode
2011-02-04 15:25 ` Inspecting integer, float values from hexl-mode Stefan Monnier
@ 2011-02-11 16:02 ` Alberto Luaces
0 siblings, 0 replies; 4+ messages in thread
From: Alberto Luaces @ 2011-02-11 16:02 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-02-11 16:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.1.1296724832.13967.help-gnu-emacs@gnu.org>
2011-02-04 15:25 ` Inspecting integer, float values from hexl-mode Stefan Monnier
2011-02-11 16:02 ` Alberto Luaces
2011-02-04 15:38 ` despen
2011-02-03 9:20 Alberto Luaces
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).