0

I have a long hex string, e.g, '0x0000fffffffffffff000000000ffffff'.

How to convert it to exact the same hex number: 0x0000fffffffffffff000000000ffffff

MK 5012
  • 29
  • 1
  • 9

1 Answers1

0
>>> eval('0x0000fffffffffffff000000000ffffff')
5192296858534826475608991739150335
>>> hex(eval('0x0000fffffffffffff000000000ffffff'))
'0xfffffffffffff000000000ffffff' #unnecessary zeros is omitted here

But if you need to get a number with fixed length of 32 digits, here is solution:

def hex_op(hex_number):
    len_ = len(hex_number[2:])
    if len_ < 32: #checking if number has less than 32 digits
        return "0x" + "0"*(32-len_) + hex_number[2:]
    else:
        return hex_number

And usage example:

>>> hex_op('0xfffffffffffff000000000ffffff') # our number that we got from previous example
'0x0000fffffffffffff000000000ffffff'
MaxLunar
  • 653
  • 6
  • 24