Last active
August 2, 2021 07:14
-
-
Save zhengfan2014/02746dc09462fbf25a7751832ad4799d to your computer and use it in GitHub Desktop.
进制转换,二进制与十进制互转_python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# eg: Twelve(29) => [1,1,1,0,1] | |
def Twelve(self,num): | |
result = [] | |
while num>2: | |
result.append(num%2) | |
num = math.floor(num/2) | |
result.append(num%2) | |
return result[::-1] | |
# eg: Lists2StringTen([1,1,1,0,1]) => 29 | |
def Lists2StringTen(self,lists): | |
lists = lists[::-1] | |
strin_ = "" | |
for i in range(len(lists)): | |
strin_ += str(lists[i]) | |
result = int(strin_,2) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment