title: How to write ANSI compatible bytes in Python 3 (0x80 and up)?
body: <p>I am new to python and try to write a script which makes byte 0x00-0xff output to a file.</p>
  
  <p>This is the code i am currently looking at</p>
  
  <pre><code>import sys
  
  filename="binarywriter.txt"
  openmode="wb"
  charset="utf8"
  
  file=open(filename, openmode)
  for x in range(16,256):
      s="0x%.2x=\\x" %x
      s+=hex(x)[2:]
      d=bytes(s, charset).decode("unicode_escape")
      file.write(d.encode(charset))
      file.write("\r\n".encode(charset))
  file.close()
  sys.stdin.readline()
  </code></pre>
  
  <p>The thing is that the python script writes all well until byte 0x7f (which is ascii) after this (0x80) it converts all output in utf8 encoding which has 2 bytes. The thing is to avoid this and only write something like this:</p>
  
  <pre><code>...
  0x7f
  0x80
  0x81
  ...
  </code></pre>
  
  <p>so i need something like 8bit encoding but after hour of searching there has not been any result for me</p>
  
  <p>there is also a little problem, where numbers &lt;16 return a single hex value which cannot be decoded, but i might find a solution for this</p>