0

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

Edited by kayoh: n/a

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by kayoh
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.