Print a variable in hexadecimal in Python

后端 未结 6 1308
盖世英雄少女心
盖世英雄少女心 2021-02-05 09:17

I\'m trying to find a way to print a string in hexadecimal. For example, I have this string which I then convert to its hexadecimal value.

my_string = \"deadbeef         


        
6条回答
  •  既然无缘
    2021-02-05 09:44

    A way that will fail if your input string isn't valid pairs of hex characters...:

    >>> import binascii
    >>> ' '.join(hex(ord(i)) for i in binascii.unhexlify('deadbeef'))
    '0xde 0xad 0xbe 0xef'
    

提交回复
热议问题