print 077777#how can i get binary
I am using python2.5
The simplest (not the fastest!) Way to get a binary string for an int in Python 2.5:
def dobin(n): digs = [] s = '' if n<0: s = '-' n = -n while True: digs.append(str(n % 2)) n /= 2 if not n: break if s: digs.append(s) digs.reverse() return ''.join(digs)
Are you looking for speed or for clarity?
Make a map of hexadecimal characters for binary sequences, then do a number through (note: only works for non-negative numbers):
def bin(value): binmap = {'0': '0000', '1': '0001', ..., 'f': '1111'} return ''.join(binmap[x] for x in ('%x' % (value,))).lstrip('0') or '0'
ActiveState Code, :
Python 2.6 , .
:print '{0:b}'.format(077777)
:
print '{0:b}'.format(077777)
n = 1234 "".join([["0", "1"][(n >> i) & 1] for i in reversed(range(n.__sizeof__()))])
, sizeof correct.you .
"".join([["0", "1"][(n>>i)&1] for i in range(log(n,2)+1)])
Source: https://habr.com/ru/post/1736703/More articles:Use array as jQuery POST variables? - javascriptDoes calling "new date (long)" result in "January 01, 01:00:00 CET 1970"? - javaWhy is OracleDataAdapter.Fill () so slow? - sqlWhat is the best python or bash for selectively concatenating a large number of files? - pythonHow can I convert an integer to 'binary' in python - pythonSearch and Match Algorithm - algorithmHow to get long url from short url - httpWhat is the most efficient way to load data from a file into a collection on demand? - javaPHPExcel Function Question - phpНевозможно загрузить DropDownList в FormView из кода? - .netAll Articles