将 base-2 二进制数字符串转换为 int [英] Convert base-2 binary number string to int

查看:30
本文介绍了将 base-2 二进制数字符串转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将基数为 2 的二进制数字符串转换为 int,如下所示:

<预><代码>>>>'11111111'.fromBinaryToInt()255

有没有办法在 Python 中做到这一点?

解决方案

你使用内置的 int() 函数,并将输入数字的基数传递给它,即 2 表示二进制数:

<预><代码>>>>int('11111111', 2)255

这里是 Python 2Python 3.

I'd simply like to convert a base-2 binary number string into an int, something like this:

>>> '11111111'.fromBinaryToInt()
255

Is there a way to do this in Python?

解决方案

You use the built-in int() function, and pass it the base of the input number, i.e. 2 for a binary number:

>>> int('11111111', 2)
255

Here is documentation for Python 2, and for Python 3.

这篇关于将 base-2 二进制数字符串转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆