二进制到整数 ->埃尔兰
我有一个二进制 M,这样 34= 将始终存在,其余的可能在任意位数之间变化,但始终是整数。
M = [<<"34=21">>]
当我运行此命令时,我得到一个答案,例如
hd([X || <<"34=", X/binary >> <- M])
Answer -> <<"21">>
如何才能使其成为一个整数,并尽可能使其高效?
I have a binary M such that 34= will always be present and the rest may vary between any number of digits but will always be an integer.
M = [<<"34=21">>]
When I run this command I get an answer like
hd([X || <<"34=", X/binary >> <- M])
Answer -> <<"21">>
How can I get this to be an integer with the most care taken to make it as efficient as possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
得到整数
21
That yields the integer
21
自 R16B 起,BIF
binary_to_integer/1
可以使用:所以看起来像:
As of R16B, the BIF
binary_to_integer/1
can be used:So that would look like:
数字的字符串表示形式可以通过 N-48 进行转换。对于多位数字,您可以折叠二进制数,乘以数字位置的幂:
其性能比使用
list_to_integer(binary_to_list(X))
选项慢大约 100 倍。A string representation of a number can be converted by N-48. For multi-digit numbers you can fold over the binary, multiplying by the power of the position of the digit:
The performance of this is around 100 times slower than using the
list_to_integer(binary_to_list(X))
option.