Print a variable in hexadecimal in Python

后端 未结 6 1373
盖世英雄少女心
盖世英雄少女心 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:39

    You can try something like this I guess:

    new_str = ""
    str_value = "rojbasr"
    for i in str_value:
        new_str += "0x%s " % (i.encode('hex'))
    print new_str
    

    Your output would be something like this:

    0x72 0x6f 0x6a 0x62 0x61 0x73 0x72
    

提交回复
热议问题