bin2intelhex.c.simple 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Quick and dirty program to make intel-hex from a binary.
  3. Written by R.E.Wolff@BitWizard.nl
  4. This file is in the public domain
  5. Typing started:
  6. Mon Jun 16 00:24:15 MET DST 1997
  7. programming stopped:
  8. Mon Jun 16 00:31:27 MET DST 1997
  9. debugging finished (2 bugs found):
  10. Mon Jun 16 00:32:52 MET DST 1997
  11. ---------------------------------------------------------
  12. Doc written in timeout. Everything else in this file was done while
  13. the timer was running.
  14. I promised "Mark Kopecki" that writing the bin-to-intel-hex
  15. converter would cost less than 15 minutes, and that it would be more
  16. trouble to find a converter on the net than to write the converter
  17. myself. I ended up spending over half an hour searching for
  18. spec/converter/docs because of unreachable hosts on the internet. I
  19. got a file with docs, after that it was 8 minutes.....
  20. ---------------------------------------------------------
  21. */
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. /* Intel Hex format:
  25. ll aaaa tt dd....dd cc
  26. ll = length
  27. aaaa = address
  28. tt = type
  29. dd....dd = data
  30. cc = checksum.
  31. */
  32. int main (int argc, char **argv)
  33. {
  34. unsigned char buf[32];
  35. int addr = 0;
  36. int n,i;
  37. while ((n = read (0, buf+4, 16)) > 0) {
  38. buf[0] = n;
  39. buf[1] = addr >> 8;
  40. buf[2] = addr & 0xff;
  41. buf[3] = 0x00;
  42. buf[4+n] = 0x00;
  43. for (i=0;i<4+n;i++)
  44. buf[4+n] -= buf[i];
  45. printf (":");
  46. for (i=0;i<= 4+n;i++)
  47. printf ("%02x", buf[i]);
  48. printf ("\n");
  49. addr += n;
  50. }
  51. printf (":0000000001ff\n");
  52. exit (0);
  53. }