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

Every line of 'convert int to bytes 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
115def bytes_to_int(bytes):
116 return reduce(lambda s, x: (s << 8) + x, bytearray(bytes))
8def _integer_to_bytes(integer, _bytes=16):
9 output = bytearray()
10 _bytes /= 2
11 for byte in range(_bytes):
12 output.append((integer >> (8 * (_bytes - 1 - byte))) & 255)
13 return output
55def int_to_bytes(int, word_size):
56 b = ''
57 while int > 0:
58 b = chr(int & 0xFF) + b
59 int = int >> 8
60 b = ((word_size - len(b)) * '\x00') + b
61 return b
226def _bytes_to_int(b):
227 return int(codecs.encode(b, "hex"), 16)
109def int_to_bytes(val, nbytes): # Convert int value to little endian bytes.
110 v = abs(val)
111 b = []
112 for n in range(nbytes):
113 b.append((v >> (8 * n)) & 0xff)
114 if val < 0:
115 for i in range(nbytes):
116 b[i] = ~b[i] + 256
117 b[0] += 1
118 for i in range(nbytes):
119 if b[i] == 256:
120 b[i] = 0
121 b[i+1] += 1
122 return bs(b)
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)
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
42def bytes_to_int(be_bytes):
43 """ Interprets a big-endian sequence of bytes as an integer. """
44 return int(hexlify(be_bytes), 16)
42def intlist_to_binary(intlist):
43 '''Convert a list of integers to a binary string type'''
44 return bytes(intlist)
162def _deserialize_int(bytes):
163 return int.from_bytes(bytes, 'big')

Related snippets