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"

Thanks,

Kayoh

ADDRESS = 0x3F8000
pk_ADDRESS = struct.pack('>l', ADDRESS)

f = open("data.bin", 'wb')
f.write(pk_ADDRESS)
f.close()

Admin, Please Delete or Close this thread, for some reason this sample code works but from my main program I get thrown the same error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.