0

I have a binary file. I want to read hexadecimal data from the terminal in my python code. I am executing the program as follows: python hello.py "2DB6C" "CDEF"

"2DB6C" :- (Address in hex) Indicates GoTo address <2DB6C> in the temp.bin file, where I want to start writing the data.

"CDEF" :- Data to be written in binary file. Remember the data is given in the hex format.

I want to write the data in the small endian format. But it is not working for me.

file = open("temp.bin", "r+b")
file.seek(4)
datatomodify = "CDEF"
data = binascii.unhexlify(datatomodify)
print ("data :", data, "offset addr :", hex(file.tell()))
file.write(data)
print ("after writing addr :", hex(file.tell()))
file.close()

It is writing in the file as "CDEF". But I want to write the data in the little endian format.

Kindly help me in fixing it.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
ANURAG MODI
  • 33
  • 1
  • 7
  • https://stackoverflow.com/questions/13155570/reorder-byte-order-in-hex-string-python – Joe Aug 06 '18 at 05:31
  • `data` has type `bytes`. A stream of bytes doesn't have an endianess, because all the values in the object are 8 bit. Endianess applies to, for example, 16 bit and 32 bit integers. For those, the endianess specifies the order of the bytes used to represent the integers. – Warren Weckesser Aug 06 '18 at 06:01

0 Answers0