简体   繁体   English

如何将 python HEX 输出转换为 ASCII?

[英]How to convert python HEX ouput to ASCII?

I'd like to convert HEX output in python to ASCII when i'm using LiveCapture from pyshark.当我使用 pyshark 的 LiveCapture 时,我想将 python 中的 HEX output 转换为 ASCII。

My code:我的代码:

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("")

Output for receive packets: Output 用于接收数据包:

66787874798582124495051

I'd like to convert this output to ASCII char directly in the python output Is it possible?我想在 python output 中直接将这个 output 转换为 ASCII char 是否可能?

Thanks谢谢

Yes, you can convert it directly.是的,你可以直接转换它。

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))

Refer link, which convert HEX to ASCII online.参考链接,在线将 HEX 转换为 ASCII。https://www.rapidtables.com/convert/number/ascii-to-hex.html You can verify the output manually and confirm the result.https://www.rapidtables.com/convert/number/ascii-to-hex.html您可以手动验证output并确认结果。

Similar code is available on: https://www.geeksforgeeks.org/convert-hexadecimal-value-string-ascii-value-string/类似代码可在: https://www.geeksforgeeks.org/convert-hexadecimal-value-string-ascii-value-string/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM