How to write a raw hex byte to stdout in Python 3?

前端 未结 3 1790
一整个雨季
一整个雨季 2020-12-09 22:39

How do you get Python 3 to output raw hexadecimal byte? I want to output the hex 0xAA.

If I use print(0xAA), I get the ASCII \'170\'.

3条回答
  •  自闭症患者
    2020-12-09 23:21

    The solution was to first create a bytes object:

    x = bytes.fromhex('AA')
    

    And then output this to stdout using the buffered writer

    sys.stdout.buffer.write(x)
    

提交回复
热议问题