How to convert an int to a hex string?

后端 未结 13 726
不知归路
不知归路 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:40

    You are looking for the chr function.

    You seem to be mixing decimal representations of integers and hex representations of integers, so it's not entirely clear what you need. Based on the description you gave, I think one of these snippets shows what you want.

    >>> chr(0x65) == '\x65'
    True
    
    
    >>> hex(65)
    '0x41'
    >>> chr(65) == '\x41'
    True
    

    Note that this is quite different from a string containing an integer as hex. If that is what you want, use the hex builtin.

提交回复
热议问题