简体   繁体   中英

How to convert dec to hex in python?

When I try to convert big numbers I get extra numbers at the end that doesn't move plus an L character too. How to remove the 4 extra characters at the end 000L?

8b8dbbc584d9c000L

8b8dc4ddd34c6000L

8b8dcdf621bf0000L

8b8dd70e7031a000L

8b8de026bea44000L

#!/usr/bin/python

import sys

def int_tuple_from_cmdline():
  """return exactly two integers form sys.argv
     or die with an error message
  """
  import sys
  args = sys.argv[1:] # drop first entry (progpath)
  if len(args) != 2:
      raise SystemExit("\n#################################################\n# Please enter both a 
start and stop parameter. #\n#################################################")
  for i in range(len(args)):
      try:
         args[i] = int(args[i])
      except ValueError:
         raise SystemExit("\n#################################################\n# Parameter %d is not an integer. You entered: %s #\n#################################################\n" %(i+1,args[i]))
  return tuple(args)

start, stop = int_tuple_from_cmdline()

r = start - 10000000000000
while r < stop:
r = r + 50000000000000
hx = hex(r)[2:]
print(hx)

If what you're after is simply removing the last four characters of a string, you can use slicing to perform it:

s = "8b8dbbc584d9c000L"
trimmed = s[:-4] #8b8dbbc584d9c

Note that it will produce an empty string if the original string doesn't have at least four characters.

Also note that my answer has nothing to do with conversion from decimal to hex per se , I'm just answering the trimming part of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM