How to convert an int to a hex string?

后端 未结 13 666
不知归路
不知归路 2020-11-28 03:27

I want to take an integer (that will be <= 255), to a hex string representation

e.g.: I want to pass in 65 and get out \'\\x41\', or

相关标签:
13条回答
  • 2020-11-28 03:57

    Note that for large values, hex() still works (some other answers don't):

    x = hex(349593196107334030177678842158399357)
    print(x)
    

    Python 2: 0x4354467b746f6f5f736d616c6c3f7dL
    Python 3: 0x4354467b746f6f5f736d616c6c3f7d

    For a decrypted RSA message, one could do the following:

    import binascii
    
    hexadecimals = hex(349593196107334030177678842158399357)
    
    print(binascii.unhexlify(hexadecimals[2:-1])) # python 2
    print(binascii.unhexlify(hexadecimals[2:])) # python 3
    
    0 讨论(0)
提交回复
热议问题