在状态栏中显示十六进制值的二进制版本

时间:2014-11-26 17:48:41

标签: emacs cc-mode

我现在正在做很多嵌入式C编程,这意味着我一直在写这样的东西:

(ioe_extra_A & 0xE7)

如果将光标放在0xE7上,emacs会在状态栏或迷你缓冲区中显示“0b1110 0111”,这将是非常有用的,所以我可以检查我的掩码是我的意思。 / p>

通常,无论我想要emacs做什么,10分钟的谷歌搜索都能找到答案,但对于这一点,我已经用尽了我的搜索技巧,仍然没有找到答案。

提前致谢。

1 个答案:

答案 0 :(得分:3)

这似乎有效:

(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's 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))))))

输入M-x my-hex-idle-status-on将其打开。

如上所述,感谢Trey Jacksonint-to-binary-string