WriteFileHEX function

extract code form main function to new WriteFileHEX function