byte oriented programming Flashcards
~240
-241
how to get complimentary
bit(~0b11110000&0b11111111)
(1 «_space;a.bit_length()) - 1 - a
get number of bits for integer
int(256).bit_length()
int to bytes
int(0xcafebabe).to_bytes(length=4, byteorder=’big’)
int(0xcafebabe).to_bytes(length=4, byteorder=’little’)
get native byteorder
sys.byteorder
get int from bytes
int.from_bytes(bytes, byteorder=”little”)
byte string from literal
b’some string’
create bytes with several elements
bytes(5)
create bytes from iterables of integers
bytes([55,34,22,254])
create bytes from string
bytes(string, encoding=”utf16”)
create bytes from hex string
bytes.fromhex(‘543433434fb0323’)
create mutable bytearray
bytearray()
module for working with C structures
struct
get items from bytes
struct.unpack_from(‘@fffHHH’, buffer)
@ - native byte order
get iterables of items from bytes
struct.iter_unpack(‘@3f3H, buffer’)
get hex representation of string
binascii.hexlify(string)
create memoryview
memoryview(bytes)
interpreter the memoryview
mem[12:18].cast(‘H’).tolist()
get memorymap file
with mmap.mmap(file.fileno(), 0 , access=mmap.ACCESS_READ ) as buffer: