Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

binary to c source converter #2

Open
noemu opened this issue Jun 12, 2018 · 1 comment
Open

binary to c source converter #2

noemu opened this issue Jun 12, 2018 · 1 comment

Comments

@noemu
Copy link

noemu commented Jun 12, 2018

if someone is interested in an automatic python script to convert binary files to c source:

import sys
import binascii
import os
def printProgressBar(progress):
    i = int(progress * 20)
    sys.stdout.write('\r')
    sys.stdout.write("[%-20s] %d%%" % ('='*i, 5*i))
    sys.stdout.flush()

def openFileToByte_generator(filename , chunkSize = 128):
    fileSize = os.stat(filename).st_size
    readBytes = 0.0
    with open(filename, "rb") as f:
        while True:
            chunk = f.read(chunkSize)
            readBytes += chunkSize
            printProgressBar(readBytes/float(fileSize))
            if chunk:
                for byte in chunk:
                    yield byte
            else:
                break


if(len(sys.argv) is not 2):
	sys.exit('usage: binConverter.py "pathToFile\\fileName.bin"')

fileIn = sys.argv[1]


base = os.path.splitext(fileIn)[0]
fileOut =  base + ".h"

stringBuffer = "\t"
countBytes = 0
print("reading file: " + fileIn)

for byte in openFileToByte_generator(fileIn,16):
    countBytes += 1
    stringBuffer += "0x"+binascii.hexlify(byte).decode('ascii')+", "
    if countBytes%16 is 0:
    	stringBuffer += "\n\t"



stringBuffer = "#include <Arduino.h> \n \n#define FUSEE_BIN_SIZE " + str(countBytes) + "\nconst PROGMEM byte fuseeBin[FUSEE_BIN_SIZE] = {\n" + stringBuffer + "\n};"

print("\nwriting file: " + fileOut)
text_file = open(fileOut, "w")
text_file.write(stringBuffer)
text_file.close()
print("finished")
@aveao
Copy link

aveao commented Aug 16, 2018

Thanks a ton for this!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants