我想把一个数字转换成二进制字符串,例如(到-二进制11) -> "1011“。
我已经找到了一种转换为十六进制和oct的方法:
(format "%x" 11) -> "B"
(format "%o" 11) -> "13"但是二进制文件显然没有格式字符串("%b“给出了一个错误)。
相反,转换很简单:(字符串到数字"1011“2) -> 11。
还有其他的库函数可以这样做吗?
发布于 2013-12-13 22:49:01
虽然我同意这是一个重复的功能,如果你想知道如何在Emacs中做位旋转,你可以在按位运算上阅读手册。这可能导致这样的实现:
(defun int-to-binary-string (i)
"convert an integer into it's binary representation in string format"
(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))https://stackoverflow.com/questions/20568684
复制相似问题