python二进制转为十进制
时间: 2023-10-02 13:10:32 浏览: 326
在Python中,可以使用int()函数将二进制转换为十进制。首先,将二进制数作为字符串提供给int()函数,然后设置第二个参数为2,表示将字符串解释为二进制数。以下是一个示例:
binary_str = '1010'
decimal_num = int(binary_str, 2)
print(decimal_num)
请注意,上述代码中的binary_str是一个二进制字符串,而不是一个整数。函数int()将其解释为一个二进制数,并将其转换为对应的十进制数值。输出结果将是十进制数值10。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Python中二进制十进制转换](https://blog.csdn.net/2201_75412958/article/details/128751153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [python 实现二进制和十进制相互转换](https://blog.csdn.net/qq_43252908/article/details/100821961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文