【问题标题】:Display the binary version of hex value in status bar在状态栏中显示十六进制值的二进制版本
【发布时间】:2014-11-27 02:03:41
【问题描述】:

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

(ioe_extra_A & 0xE7)

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

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

提前谢谢。

【问题讨论】:

  • 出于好奇:那为什么写 0xE7 而不是 0b11100111?
  • 好主意,但“0b”表示法是not standard C,我的编译器不支持它。

标签: emacs cc-mode


【解决方案1】:

这似乎有效:

(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 Jackson int-to-binary-string

【讨论】:

  • 这正是我所需要的,谢谢!顺便说一句,这有点漂亮: (defun int-to-binary-string (i) "convert an integer into it's binary representation in string format By Trey Jackson, from stackoverflow.com/a/20577329." (let ((res "") (cnt 0)) (while (not (= i 0)) (setq res (concat (if (= 1 (logand i 1)) "1" "0") res)) (setq i (lsh i -1)) (if (= cnt 3) (setq res (concat " " res))) (setq cnt (+ cnt 1)) ) (if (string= res "") (setq res "0")) res ))
猜你喜欢
  • 2014-03-11
  • 2014-10-30
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2012-05-24
  • 2020-06-19
相关资源
最近更新 更多