[英]Convert binary string to number
Pretty straightforward, but I can't seem to find an answer.很简单,但我似乎找不到答案。 I have a string of 1s and 0s such as "01001010"
- how would I parse that into a number?我有一个由 1 和 0 组成的字符串,例如"01001010"
——我如何将其解析为一个数字?
Use string-to-number
, which optionally accepts the base:使用string-to-number
,它可以选择接受基数:
(string-to-number "01001010" 2)
;; 74
As explained by @sds in a comment , string-to-number
returns 0 if the conversion fails.正如@sds 在评论中所解释的,如果转换失败, string-to-number
将返回 0。 This is unfortunate, since a return value of 0 could also means that the parsing succeeded.这是不幸的,因为返回值 0 也可能意味着解析成功。 I'd rather use the Common Lisp version of this function, cl-parse-integer
.我宁愿使用这个函数的 Common Lisp 版本cl-parse-integer
。 The standard function is described in the Hyperspec , whereas the one in Emacs Lisp is slightly different (in particular, there is no secondary return value): Hyperspec 中描述了标准函数,而 Emacs Lisp 中的函数略有不同(特别是,没有辅助返回值):
(cl-parse-integer STRING &key START END RADIX JUNK-ALLOWED)
Parse integer from the substring of STRING from START to END .从STRING的子字符串从START到END解析整数。 STRING may be surrounded by whitespace chars (chars with syntax
' '
). STRING可能被空格字符(具有语法' '
字符)包围。 Other non-digit chars are considered junk.其他非数字字符被视为垃圾。 RADIX is an integer between 2 and 36, the default is 10. Signal an error if the substring between START and END cannot be parsed as an integer unless JUNK-ALLOWED is non-nil. RADIX是 2 到 36 之间的整数,默认值为 10。如果START和END之间的子字符串无法解析为整数,除非JUNK-ALLOWED非零,否则会发出错误信号。
(cl-parse-integer "001010" :radix 2)
=> 10
(cl-parse-integer "0" :radix 2)
=> 0
;; exception on parse error
(cl-parse-integer "no" :radix 2)
=> Debugger: (error "Not an integer string: ‘no’")
;; no exception, but nil in case of errors
(cl-parse-integer "no" :radix 2 :junk-allowed t)
=> nil
;; no exception, parse as much as possible
(cl-parse-integer "010no" :radix 2 :junk-allowed t)
=> 2
This thread has an elisp tag.这个线程有一个elisp 标签。 Because it also has a lisp tag, I would like to show standard Common Lisp versions of two solutions.因为它也有一个 lisp 标签,所以我想展示两个解决方案的标准 Common Lisp 版本。 I checked these on LispWorks only.我只在 LispWorks 上检查过这些。 If my solutions are not standard Common Lisp, maybe someone will correct and improve my solutions.如果我的解决方案不是标准的 Common Lisp,也许有人会纠正和改进我的解决方案。
For solutions对于解决方案
(string-to-number "01001010" 2)
and和
(cl-parse-integer "001010" :radix 2)
LispWorks does not have string-to-number and does not have cl-parse-integer . LispWorks 没有string-to-number并且没有cl-parse-integer 。 In LispWorks, you can use:在 LispWorks 中,您可以使用:
(parse-integer "01001010" :radix 2)
For the solution对于解决方案
(read (concat "#2r" STRING))
LispWorks does not have concat . LispWorks 没有concat 。 You can use concatenate instead.您可以改用连接。 read won't work on strings in LispWorks. read不适用于 LispWorks 中的字符串。 You have to give read a stream.你必须给read一个流。
In LispWorks, you can do this:在 LispWorks 中,您可以这样做:
(read (make-string-input-stream (concatenate 'string "#2r" "01001010")))
You can also use format like this:您还可以使用这样的格式:
(read (make-string-input-stream (format nil "#2r~a" "01001010")))
This seems hacky by comparison, but FWIW you could also do this:相比之下,这似乎很笨拙,但 FWIW 你也可以这样做:
(read (concat "#2r" STRING))
ie read
a single expression from STRING as a binary number.即从字符串中read
单个表达式作为二进制数。
This method will signal an error if the expression isn't valid.如果表达式无效,此方法将发出错误信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.