Skip to content

Instantly share code, notes, and snippets.

@TheFlash2k
Last active July 7, 2023 02:38
Show Gist options
  • Save TheFlash2k/15a45723cbab870dac0478e017890fb4 to your computer and use it in GitHub Desktop.
Save TheFlash2k/15a45723cbab870dac0478e017890fb4 to your computer and use it in GitHub Desktop.
A simple script to convert an hex to bytes based on endianess.
#!/usr/bin/env python3
import argparse
import struct
def pack_address(address, arch, byte_order):
_ = { 'x64' : { 'little' : '<Q', 'big' : '>Q' }, 'x86' : { 'little' : '<I', 'big' : '>Q' } }
return struct.pack(_[arch][byte_order], address)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--type', choices=['little', 'big'], default='little', help='Specify little or big')
parser.add_argument('address', type=str, help='Address')
parser.add_argument('-a', '--arch', choices=['x86', 'x64'], default='x86', help='Specify x86 or x64 (default: x86)')
args = parser.parse_args()
address_packed = pack_address(int(args.address, 16), args.arch, args.type)
print(f'Packed address: {address_packed}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment