Display binary version of hex value in status bar
I am doing a lot of embedded C programming right now, which means I write things like this all the time:
(ioe_extra_A & 0xE7)
It would be very helpful if I put the cursor at 0xE7 emacs would display "0b1110 0111" in the status bar or minibuffer, so I could check that my mask is what I meant.
Generally, no matter what I want to do emacs, 10 minutes of Googling will return an answer, but for that I have exhausted my search skills and still have not received an answer.
Thanks in advance.
+3
source to share
1 answer
It works:
(defvar my-hex-idle-timer nil)
(defun my-hex-idle-status-on ()
(interactive)
(when (timerp my-hex-idle-timer)
(cancel-timer my-hex-idle-timer))
(setq my-hex-idle-timer (run-with-idle-timer 1 t 'my-hex-idle-status)))
(defun my-hex-idle-status-off ()
(interactive)
(when (timerp my-hex-idle-timer)
(cancel-timer my-hex-idle-timer)
(setq my-hex-idle-timer nil)))
(defun int-to-binary-string (i)
"convert an integer into it binary representation in string format
By Trey Jackson, from https://stackoverflow.com/a/20577329/."
(let ((res ""))
(while (not (= i 0))
(setq res (concat (if (= 1 (logand i 1)) "1" "0") res))
(setq i (lsh i -1)))
(if (string= res "")
(setq res "0"))
res))
(defun my-hex-idle-status ()
(let ((word (thing-at-point 'word)))
(when (string-prefix-p "0x" word)
(let ((num (ignore-errors (string-to-number (substring word 2) 16))))
(message "In binary: %s" (int-to-binary-string num))))))
Enter M-x my-hex-idle-status-on
to enable it.
As noted, thanks to Trey Jackson for int-to-binary-string
.
+3
source to share