首页 > 解决方案 > python format()结合join和数据转换

问题描述

这个 Python 代码正是我想要的:

a_hex_str = ':'.join('%02x' % b for b in a_bytes)

它是以十六进制打印的字节数组 a_bytes 中的字节值的字符串,每个值之间有一个冒号。

我一直在疯狂尝试使用新的 format() 函数代替旧的 % 来获得相同的输出,但我所有的尝试都给出了错误。

我将非常感谢您的帮助。

标签: python-3.xjoinformat

解决方案


a_hex_str = ':'.join('{}02x'.format(b) for b in a_bytes)

试试这条线,希望它能奏效。


推荐阅读