22

I am trying to convert big integer number to hexadecimal, but in result I get extra "0x" in the beginning and "L" at the and. Is there any way to remove them. Thanks. The number is:

44199528911754184119951207843369973680110397865530452125410391627149413347233422
34022212251821456884124472887618492329254364432818044014624401131830518339656484
40715571509533543461663355144401169142245599341189968078513301836094272490476436
03241723155291875985122856369808620004482511813588136695132933174030714932470268
09981252011612514384959816764532268676171324293234703159707742021429539550603471
00313840833815860718888322205486842202237569406420900108504810

In hex I get:

0x2ef1c78d2b66b31edec83f695809d2f86e5d135fb08f91b865675684e27e16c2faba5fcea548f3
b1f3a4139942584d90f8b2a64f48e698c1321eee4b431d81ae049e11a5aa85ff85adc2c891db9126
1f7f2c1a4d12403688002266798ddd053c2e2670ef2e3a506e41acd8cd346a79c091183febdda3ca
a852ce9ee2e126ca8ac66d3b196567ebd58d615955ed7c17fec5cca53ce1b1d84a323dc03e4fea63
461089e91b29e3834a60020437db8a76ea85ec75b4c07b3829597cfed185a70eeaL
1

6 Answers 6

61

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'
2
  • 16
    Or {0:x}.format(integer) using new-style formatting. Add a # before the x to get a 0x prefix.
    – agf
    Commented Apr 18, 2012 at 22:14
  • 1
    Yeah, I think that's actually the best answer.
    – kindall
    Commented Apr 19, 2012 at 14:43
41

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.)

1
  • 2
    I'd suggest that Alex's answer below is the best. Simply format(bignum, 'x'). That doesn't have issues with stripping leading zeros, and all in all it's simpler than new style string formatting.
    – ketil
    Commented May 2, 2018 at 7:23
11

Similar to Praveen's answer, you can also directly use built-in format().

>>> a = 44199528911754184119951207843369973680110397
>>> format(a, 'x')
'1fb62bdc9e54b041e61857943271b44aafb3d'
2

I think it's dangerous idea to use strip.
because lstrip or rstrip strips 0.

ex)

a = '0x0'
a.lstrip('0x')  

''

result is '', not '0'.

In your case, you can simply use replace to prevent above situation.
Here's sample code.

hex(bignum).replace("L","").replace("0x","")
0

Be careful when using the accepted answer as lstrip('0x') will also remove any leading zeros, which may not be what you want, see below:

>>> account = '0x000067'
>>> account.lstrip('0x')
'67'
>>>

If you are sure that the '0x' prefix will always be there, it can be removed simply as follows:

>>> hex(42)
'0x2a'
>>> hex(42)[2:]
'2a'
>>>

[2:] will get every character in the string except for the first two.

1
  • if you are sure there is always '0x' in the front, how will it remove leading 0's? Commented May 24 at 13:02
-2

A more elegant way would be

hex(_number)[2:-1]

but you have to be careful if you're working with gmpy mpz types, then the 'L' doesn't exist at the end and you can just use

hex(mpz(_number))[2:]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.