How to convert an integer to hexadecimal without the extra '0x' leading and 'L' trailing characters in Python?
The 0x
is literal representation of hex numbers. And L
at the end means it is a Long integer.
If you just want a hex representation of the number as a string without 0x
and L
, you can use string formatting with %x
.
>>> a = 44199528911754184119951207843369973680110397
>>> hex(a)
'0x1fb62bdc9e54b041e61857943271b44aafb3dL'
>>> b = '%x' % a
>>> b
'1fb62bdc9e54b041e61857943271b44aafb3d'
Sure, go ahead and remove them.
hex(bignum).rstrip("L").lstrip("0x") or "0"
(Went the strip()
route so it'll still work if those extra characters happen to not be there.)
Similar to Praveen's answer, you can also directly use built-in format()
.
>>> a = 44199528911754184119951207843369973680110397
>>> format(a, 'x')
'1fb62bdc9e54b041e61857943271b44aafb3d'