Search code examples
pythonencodinghexbyteansi

How to write ANSI compatible bytes in Python 3 (0x80 and up)?


I am new to python and try to write a script which makes byte 0x00-0xff output to a file.

This is the code i am currently looking at

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()

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:

...
0x7f
0x80
0x81
...

so i need something like 8bit encoding but after hour of searching there has not been any result for me

there is also a little problem, where numbers <16 return a single hex value which cannot be decoded, but i might find a solution for this


Solution

  • You can use Latin-1 (also known as ISO_8859-1) encoding.

    charset="iso-8859-1"
    

    See the screenshot for the proof that every character only needs one byte in the file: Screenshot of hexeditor showing file