freshnewpage freshnewpage - 1 month ago 12
Python Question

How to write a raw hex byte to stdout in python 3

How to you get python3 to output raw hexadecimal byte.

I want to output the hex

0xAA
.

If I use:

print(0xAA)


I get the ASCII '170'

Answer

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)