10 examples of 'python int to binary' in Python

Every line of 'python int to binary' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
42def intlist_to_binary(intlist):
43 '''Convert a list of integers to a binary string type'''
44 return bytes(intlist)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

53def to_binary(n, result=""):
54 if n < 2:
55 return str(n) + result
56 return to_binary(n/2, str(n%2) + result)
9def to_binary(x): return '' if x == 0 else to_binary(int(x / 256)) + chr(x % 256)
56def bin2int(N):
57
58 L = int(len(str(N))) #將輸入的二進位數字變成字串並數出他的長度
59 A = 0 #設定兩個變數,其中A是用來計算最後的答案,K是用來計算迴圈運行的次數以及作為2的指數
60 K = 0
61 while L > K : #設定迴圈條件,當執行的次數等於輸入的二進位數字長度後,便不再執行迴圈
62 r = int(N%10) #設定一個變數r,使其成為當前二進位數最右邊的數的值
63 A = A + (2**K)*r #計算當前所得到的數的總和
64 N = N/10 #將當前二進位數最右邊的數值去掉
65 K = K + 1 #執行次數加1
66
67 str(A) #將最後的答案轉換成字串形式
68 return A
73def bytes_from_int(val):
74 buf = []
75 while val:
76 val, remainder = divmod(val, 256)
77 buf.append(remainder)
78
79 buf.reverse()
80 return struct.pack('%sB' % len(buf), *buf)
36def frombin(v):
37 """MSB to LSB binary form"""
38 return int("".join(map(str, v)), 2 )
74def toBinary(number, zfill = 7):
75 return list(str(bin(number))[2:].zfill(zfill))
226def _bytes_to_int(b):
227 return int(codecs.encode(b, "hex"), 16)
111def binaryToLong(s):
112 return pickle.decode_long(''.join(reversed(s)))
42def bytes_to_int(be_bytes):
43 """ Interprets a big-endian sequence of bytes as an integer. """
44 return int(hexlify(be_bytes), 16)

Related snippets