Python String to hex -


so, i've been banging head against wall long on seems should easy data conversion. writing in python , passing module hex value converted wrapper c type uint_64t. problem getting hex value via python library argparse. when takes in value, example lets use value 0x3f, saves string. if try cast int throws error:

"valueerror: invalid literal int() base 10: '0x3f'" 

if create variable hex = 0x3f however, when print out, gives me appropriate integer value. (which great since i'm creating uint) confused how make conversion string int if cast doesn't work. have seen plenty of examples on turning string hex value letter (in other words take each individual character of ascii string '0x3f' , give hex value) haven't been able find example of situation looking for. apologies if i'm bringing has been answered time , again.

try specifying int in base 16:

>>> int("0x3f", 16) 63 

you use ast.literal_eval, should able read string used integer literal. don't have specify base one, long 0x prefix present.

>>> import ast >>> #hexadecimal >>> ast.literal_eval("0x3f") 63 >>> #binary >>> ast.literal_eval("0b01010") 10 >>> #octal >>> ast.literal_eval("0712") 458 >>> #decimal >>> ast.literal_eval("42") 42 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -