Skip to content

Instantly share code, notes, and snippets.

@zhengfan2014
Last active August 2, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhengfan2014/02746dc09462fbf25a7751832ad4799d to your computer and use it in GitHub Desktop.
Save zhengfan2014/02746dc09462fbf25a7751832ad4799d to your computer and use it in GitHub Desktop.
进制转换,二进制与十进制互转_python
# 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