Pravin Virkud
Pravin Virkud

Reputation: 107

How to create a dump file in hex format from python

I have a array of integer which I want to dump in one binary file (HEX file to be specific) using python script I have written a code as

MemDump = Debug.readMemory(ic.IConnectDebug.fRealTime, 0, 0xB0009CC4, 0xCFF, 1)

    MemData = MemDump[:3321]
    # Create New file in binary mode and open for writing
    fp = open("MON.dmp", 'w')
    sys.stdout = fp
    for byte in MemData:
        print(byte)

Here MemDump contains an array of integer values. From this array first 3321 bytes I want to dump in file. Here I am getting the the output in file MON.dmp but in ASCII fromat. and if I create file in binary format using

fp = open("MON.dmp", 'wb')

print(byte) command gives me an error saying

'str' does not support the buffer interface

Thank you in Advance.

Upvotes: 0

Views: 1807

Answers (1)

Dartmouth
Dartmouth

Reputation: 1089

You need to convert byte to a binary string before you can write it to a file opened in 'wb' mode. This can be done using the bytearray() function. So in this case you should use:

for byte in MemData:
    print(bytearray(byte))

Upvotes: 1

Related Questions