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 = int(args)
          except ValueError:
             raise SystemExit("\n#################################################\n# Parameter %d is not an integer. You entered: %s #\n#################################################\n" %(i+1,args))
      return tuple(args)

    start, stop = int_tuple_from_cmdline()

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

Are you sure the zeros are not required because the middle one of the hex' values has 4 of them and the 'L' stands for LONG?
Also, as a guess, you must be using Python Version 2.x.x possibly 2.7.x and not 3.x.x.

So could you confirm your Python Version and OS it is running on...

I am guessing that your decimal number was:

>>> int(0x8b8dbbc584d9c000L)
10055900000000000000L
>>> hex(10055900000000000000L)
'0x8b8dbbc584d9c000L'
>>> _

Yes I'm using python.2.7 on Ubuntu and Centos

I edited my previous post to show you, and you do not need to remove the 'L' as conversion to either format is unaffected by the letter.

It also looks as though the zeros ARE required so if it necessary to remove the 'L' then let us know...

Bazza...

Yes I want the extra characters to be remove because I'm running this script against another script. It needs to be on hex form.

Try:

hx = hex(r)[2:][:-1]

To remove the 'L'. You remove the zeros at your own risk...

Thanks that did remove the L.

Because I quoted that you remove the zeros at your own risk and did NOT put [:-4] at the end.
Again you remove those trailing zeros at YOUR OWN RISK, and that the middle one of your five results has 4 zeros and not 3 zeros.
This should be a pointer to tell you not to.

EDIT:
Please do not edit your post after a reply without letting people know what you have done in the edit box at the top of each post.
No one knows that you wanted to remove the zeros again and makes my reply look foolish...

Thank you...

1 Like

I put [:-6] and it did remove the extra zeros but I had to expand the space to get the desired numbers of characters I wanted.

You can shorten the cutting like this:
(Longhand using Python 2.7.x, OSX 10.14.6, default bash terminal.)

Last login: Mon Oct 14 15:15:55 on ttys000
AMIGA:amiga~> python
Python 2.7.10 (default, Feb 22 2019, 21:55:15) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x="0xabcdef123456"
>>> x=x[2:-6]
>>> print x
abcdef
>>> _