capa icon indicating copy to clipboard operation
capa copied to clipboard

consider replacing binascii and struct with native Python methods

Open fariss opened this issue 1 year ago • 0 comments

Python offers native methods for string-byte conversion, eliminating the need for binascii. We should replace binascii.hexlify() with bytes.hex() and binascii.unhexlify() with bytes.fromhex(). https://docs.python.org/3/library/stdtypes.html#bytes.hex

also consider, replacing struct.pack / stuct.unpack() with native to_bytes / from_bytes methods:

struct.pack("<B", op_val) ⟶ op_val.to_bytes(1, byteorder='little', signed=False)
struct.pack("<H", op_val) ⟶ op_val.to_bytes(2, byteorder='little', signed=False)
struct.pack("<I", op_val) ⟶ op_val.to_bytes(4, byteorder='little', signed=False)
struct.pack("<Q", op_val) ⟶ op_val.to_bytes(8, byteorder='little', signed=False)

# similarly
struct.unpack(">I", x)[0] ⟶ int.from_bytes(x, 'little')

https://docs.python.org/3/library/stdtypes.html#int.to_bytes

fariss avatar Aug 23 '24 00:08 fariss