0

So I made a script where you put hex data in for something like an image or an audio file inside a variable and it then copies it into its own file (self extracts). However it doesn't work with a file over 2MB.

I've tested it with files up to 2MB and it works fine but when the hex data size is above 2MB it doesn't write the full output to the file and you end up with a file that's a fraction of the original size. Here is my code (the example hex data is just random):

import os
import binascii

def save_image(filename, hex_imgdata):
    if os.path.exists(filename):
        current_file = open(filename, "wb")
    else:
        current_file = open(filename, "xb")
    decoded_imgdata = bytes.fromhex(hex_imgdata)
    current_file.write(decoded_imgdata)
    current_file.close()
    print("Saved " + filename)

example_hex = "A2C688B272"

if not os.path.exists("extracted"):
    os.mkdir("extracted")
os.chdir("extracted")

save_image(filename = "example.png", hex_imgdata = example_hex)

How can I make it work with larger files?

11
  • Cannot reproduce. I changed your example to example_hex = "A2" * 3_000_000 and got a 3,000,000 byte file.
    – tdelaney
    Commented Nov 13, 2023 at 22:41
  • I also added print(os.stat("example.png").st_size) to view the size. Can you make the same change and test so that we are working with identical code?
    – tdelaney
    Commented Nov 13, 2023 at 22:43
  • I tested with a 6 million character file and it only gave me less than half of it, try just copying the hex data of a random 6MB file and putting it in, also i will add that
    – YupYupTRP
    Commented Nov 13, 2023 at 22:45
  • 1
    The code you posted writes a 5 byte file. Can you post the code that fails? It should just be a larger example_hex string like what I suggested.
    – tdelaney
    Commented Nov 13, 2023 at 22:46
  • The written file will be half the size of the string because of the hex to binary conversion.
    – tdelaney
    Commented Nov 13, 2023 at 22:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.