python hex value as input integers

0

I'm trying to call a python script with a hexadecimal address, then add some offset to it:

import argparse
def main(number):
    print(number+1234)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument("--input",type=int)
    main(number=parser.parse_args().input)

Unfortunately, I have a type mismatch I can't seem to fix:

$ python hex.py --input=0x000000ab
usage: hex.py [-h] [--input INPUT]
hex.py: error: argument --input: invalid int value: '0x000000ab'

I must be missing some straightforward solution here.

python
argparse
asked on Stack Overflow Oct 11, 2020 by OrenIshShalom

1 Answer

0

This is a variation of the common FAQ "a string is not a number". You have to convert it, propably after skipping the 0x prefix.

print(int(number.replace("0x", ""),16)+1234)
answered on Stack Overflow Oct 11, 2020 by tripleee

User contributions licensed under CC BY-SA 3.0