将字符串打印为十六进制字节 [英] Print a string as hexadecimal bytes
本文介绍了将字符串打印为十六进制字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下字符串: Hello,World!
,我想使用Python将其打印为'48:65:6c:6c:6f:2c:20:57:6f:72:6c:64:21'.
I have this string: Hello, World!
and I want to print it using Python as '48:65:6c:6c:6f:2c:20:57:6f:72:6c:64:21'.
hex()
仅适用于整数.
怎么办?
推荐答案
您可以将字符串转换为整数生成器.对每个元素应用十六进制格式,并插入分隔符:
You can transform your string to an integer generator. Apply hexadecimal formatting for each element and intercalate with a separator:
>>> s = "Hello, World!"
>>> ":".join("{:02x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:2c:20:57:6f:72:6c:64:21
这篇关于将字符串打印为十六进制字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文