以十六进制字节打印字符串? [英] Print a string as hex bytes?

查看:178
本文介绍了以十六进制字节打印字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串: Hello world !! ,我想用Python打印它为 48:65:6c:6c:6f: 20:77:6f:72:6c:64:20:21:21

I have this string: Hello world !! and I want to print it using Python as 48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21.

/ code>只适用于整数。

hex() works only for integers.

如何做到这一点?

How can it be done?

推荐答案

字符串转换为int生成器,对每个元素应用十六进制格式并插入分隔符:

Your can transform your string to a int generator, apply hex formatting for each element and intercalate with separator:

>>> s = "Hello world !!"
>>> ":".join("{:02x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21

这篇关于以十六进制字节打印字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆