Hey guys, I working on an assignment in which I have to write binary/hex to a file. More specifically, I have a variable that holds a memory address that needs to be written to the file big endian but every time I try to write it, python being python treats all strings as ascii (values from 0-127) so I get throw the following error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 3: ordinal not in range(128)
Is there anyway around this? Below is a small segment of the code I'm trying to implement. (In my actual program, what is being written to the file is actually a bunch of strings concatenated together in a variable, and then that variable is written, so the whole this is just 1 giant string, but I'm up for advice/suggestions)
So just to clarify, I want to write ADDRESS exactly as it is (but big endian) to the file, so if I'm reading the file in Hex from Beyond Compare (or something similar) I can see the values there, it should be "00 3F 80 00"
ADDRESS = 0x3F8000 pk_ADDRESS = struct.pack('>l', ADDRESS) f = open("data.bin", 'wb') f.write(pk_ADDRESS) f.close()