Home > Enterprise >  How to grab hex value in between a bunch of zeros and convert it to decimal?
How to grab hex value in between a bunch of zeros and convert it to decimal?

Time:05-26

I wanted to try and grab a hex value in between a bunch of zeros and convert it to decimal. Here's a sample: '00000000002E3706400000'. So I only want to grab '2E37064' and disregard everything else around it. I know to use the int() function to convert it to decimal, but when I do, it includes the leading zeros right after the actual hex value. Here's a sample of my code:

hex_val = '00000000002E3706400000'
dec_val = int(hex_val, 16)
print(dec_val)

And then here's the output:

50813862936576

The actual value I want is:

48459876

Is there an optimal way to accomplish this?

CodePudding user response:

You can use the .strip() function to remove the leading and trailing zeroes (though removing the leading zeroes here isn't technically necessary):

int(hex_val.strip('0'), 16)

This outputs:

48459876
  • Related