bin2c.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (c) 2024-2025 Le Juez Victor
  2. #
  3. # This software is provided "as-is", without any express or implied warranty. In no event
  4. # will the authors be held liable for any damages arising from the use of this software.
  5. #
  6. # Permission is granted to anyone to use this software for any purpose, including commercial
  7. # applications, and to alter it and redistribute it freely, subject to the following restrictions:
  8. #
  9. # 1. The origin of this software must not be misrepresented; you must not claim that you
  10. # wrote the original software. If you use this software in a product, an acknowledgment
  11. # in the product documentation would be appreciated but is not required.
  12. #
  13. # 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  14. # as being the original software.
  15. #
  16. # 3. This notice may not be removed or altered from any source distribution.
  17. import sys
  18. def file_to_c_hex(filename):
  19. try:
  20. with open(filename, "rb") as f:
  21. data = f.read()
  22. hex_array = ", ".join(f"0x{byte:02X}" for byte in data)
  23. print(hex_array)
  24. except FileNotFoundError:
  25. print(f"Error: File '{filename}' not found.")
  26. except Exception as e:
  27. print(f"Error: {e}")
  28. if __name__ == "__main__":
  29. if len(sys.argv) != 2:
  30. print("Usage: python script.py <file>")
  31. else:
  32. file_to_c_hex(sys.argv[1])