How to convert hex string to hex number?

前端 未结 8 1202
孤独总比滥情好
孤独总比滥情好 2020-12-09 17:36

I have integer number in ex. 16 and i am trying to convert this number to a hex number. I tried to achieve this by using hex function but whenever you provide a integer numb

相关标签:
8条回答
  • 2020-12-09 18:14

    Using the string formatters (new first, then old):

    >>> '{:x}'.format( 12345678 )
    'bc614e'
    
    >>> '%x' % ( 12345678 )
    'bc614e'
    
    0 讨论(0)
  • 2020-12-09 18:14
    def add_hex2(hex1, hex2):
        """add two hexadecimal string values and return as such"""
        return hex(int(hex1, 16) + int(hex2, 16))
    print add_hex2('0xff', '0xff')  # 0x1fe
    
    0 讨论(0)
提交回复
热议问题