【问题标题】:How to convert python HEX ouput to ASCII?如何将 python HEX 输出转换为 ASCII?
【发布时间】:2021-05-01 20:16:16
【问题描述】:

当我使用来自 pyshark 的 LiveCapture 时,我想将 Python 中的 HEX 输出转换为 ASCII。

我的代码:

capture = pyshark.LiveCapture(interface='en1',
                              bpf_filter='tcp port 5555 and len > 66',)

colored.OK("Interface bindée sur %s" % socket.gethostbyname(socket.gethostname()))

for packet in capture.sniff_continuously():
   if packet.ip.src == socket.gethostbyname(socket.gethostname()):
            colored.OK("Send packets")
   else:
            colored.OK("Receive packets")
            print(''.join(packet.data.data.split(":")))
            print("")

接收数据包的输出:

66787874798582124495051

我想直接在 python 输出中将此输出转换为 ASCII 字符 有可能吗?

谢谢

【问题讨论】:

    标签: python sockets hex ascii pyshark


    【解决方案1】:

    可以,直接转换即可。

    def convert_HEX_to_ASCII(h):
    chars_in_reverse = []
    while h != 0x0:
        chars_in_reverse.append(chr(h & 0xFF))
        h = h >> 8
    
    chars_in_reverse.reverse()
    return ''.join(chars_in_reverse)
    
    print (convert_HEX_to_ASCII(0x6176656e67657273))
    print (convert_HEX_to_ASCII(0x636f6e766572745f4845585f746f5f4153434949))
    

    参考链接,在线将 HEX 转换为 ASCII。 https://www.rapidtables.com/convert/number/ascii-to-hex.html 您可以手动验证输出并确认结果。

    类似的代码可用于: https://www.geeksforgeeks.org/convert-hexadecimal-value-string-ascii-value-string/

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 2018-08-30
      • 2015-12-19
      • 1970-01-01
      • 2015-10-30
      • 2015-08-12
      • 1970-01-01
      • 2016-09-11
      • 2013-02-28
      相关资源
      最近更新 更多