我是編程和學習python和二進制文件的新手。我想手動將整數轉換爲二進制。我試圖用列表來做。我知道有這樣做的功能,這樣做效率不高。謝謝!使用數學模塊(Python)將整數轉換爲二進制
import math
binary = []
d = int(raw_input())
stepwise = int(math.log(d, 2))
while d != 0:
x = int(math.log(d, 2))
y = 2 ** x
d -= y
if stepwise == x:
binary.append("1")
else:
binary.append("0")
stepwise -= 1
print binary
你的問題是什麼? –
有一個用於將整數轉換爲二進制字符串的標準庫函數。只需使用['bin()'](https://docs.python.org/2/library/functions.html#bin)! –