How to convert an integer to hexadecimal without the extra '0x' leading and 'L' trailing characters in Python?

后端 未结 6 1316
栀梦
栀梦 2021-02-18 14:23

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 nu

6条回答
  •  耶瑟儿~
    2021-02-18 14:37

    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 replce to prevent above situation.
    Here's sample code.

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

提交回复
热议问题