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.

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87

1 Answers1

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)
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This doesn't handle the `argparse` issue, and it's kind of a weak solution (you don't need to do the `replace` if the prefix matches the `base`, and this prevents non-hex numbers from being parsed properly). – ShadowRanger Oct 11 '20 at 13:30
  • 1
    Yeah, turn to the duplicate for a proper solution. I'm guessing a beginner solution would be more appropriate here; obviously, correctness trumps brevity and ease of understanding for production code. – tripleee Oct 11 '20 at 13:38