Maison  >  Questions et réponses  >  le corps du texte

python 如何打印bytes以16进制输出

例如

a=b'\x58\x01'
print(a)

结果:

b'X\x01'

我想打印

b'\x58\x01'

怎么格式化输出?

迷茫迷茫2741 Il y a quelques jours882

répondre à tous(2)je répondrai

  • 黄舟

    黄舟2017-04-18 10:28:57

    ''.join(map(lambda x:('/x' if len(hex(x))>=4 else '/x0')+hex(x)[2:],a))

    répondre
    0
  • 高洛峰

    高洛峰2017-04-18 10:28:57

    def trans(s):
        return "b'%s'" % ''.join('\x%.2x' % x for x in s)
    
    print(trans(b'\x58\x01'))

    répondre
    0
  • Annulerrépondre