10 examples of 'convert int to binary python' in Python

Every line of 'convert int to binary python' 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)
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)
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
292def _int_convert(value: int, bit_size: int, signed: bool) -> int:
293 value &= (1 << bit_size) - 1
294 if signed and (value & (1 << (bit_size - 1))):
295 value -= 1 << bit_size
296 return value
36def frombin(v):
37 """MSB to LSB binary form"""
38 return int("".join(map(str, v)), 2 )
111def binaryToLong(s):
112 return pickle.decode_long(''.join(reversed(s)))
92def int_from_bytes(mybytes, byteorder='big', signed=False):
93 """
94 Return the integer represented by the given array of bytes.
95 The mybytes argument must either support the buffer protocol or be an
96 iterable object producing bytes. Bytes and bytearray are examples of
97 built-in objects that support the buffer protocol.
98 The byteorder argument determines the byte order used to represent the
99 integer. If byteorder is 'big', the most significant byte is at the
100 beginning of the byte array. If byteorder is 'little', the most
101 significant byte is at the end of the byte array. To request the
102 native byte order of the host system, use `sys.byteorder' as the byte
103 order value.
104 The signed keyword-only argument indicates whether two's complement is
105 used to represent the integer.
106 """
107 if byteorder not in ('little', 'big'):
108 raise ValueError("byteorder must be either 'little' or 'big'")
109 if isinstance(mybytes, unicode):
110 raise TypeError("cannot convert unicode objects to bytes")
111 # mybytes can also be passed as a sequence of integers on Py3.
112 # Test for this:
113 elif isinstance(mybytes, collections.Iterable):
114 mybytes = bytes(mybytes)
115 b = mybytes if byteorder == 'big' else mybytes[::-1]
116 if len(b) == 0:
117 b = b'\x00'
118 # The encode() method has been disabled by newbytes, but Py2's
119 # str has it:
120 num = int(b.encode('hex'), 16)
121 if signed and (b[0] & 0x80):
122 num = num - (2 ** (len(b)*8))
123 return num
37def integer_to_binarylist(num, binlen=0, strq=False):
38 """
39 Convert an integer number to a binary number in a form of a list.
40
41 Parameters
42 ----------
43 num : int
44 Integer number.
45 binlen : int
46 Length of a list containing digits of a binary number.
47 strq : bool
48 If true then returns a string instead of a list.
49
50 Returns
51 -------
52 list
53 A list containing digits of a binary number.
54 """
55 value = format(num, '0'+str(binlen)+'b')
56 rez = value if strq else list(map(int, str(value)))
57 return rez

Related snippets