Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. diff --git a/tools/bin2header.c b/tools/bin2header.c
  2. index 246e96e..a7dbe1f 100644
  3. --- a/tools/bin2header.c
  4. +++ b/tools/bin2header.c
  5. @@ -35,6 +35,7 @@ int main(int argc, char **argv)
  6. char buf[256];
  7. FILE *fi, *fo;
  8. int i, idx, r, last_byte;
  9. + long file_size;
  10.  
  11. if (argc < 4) {
  12. fprintf(stderr, "Usage: %s <raw> <c header> <array name>\n", argv[0]);
  13. @@ -58,10 +59,24 @@ int main(int argc, char **argv)
  14. // print the array version
  15. fprintf(fo, array_header, argv[3]);
  16.  
  17. + // obtain the file size
  18. + fseek (fi , 0 , SEEK_END);
  19. + file_size = ftell(fi) - 1;
  20. + // rewind the file so it can be read again
  21. + fseek(fi, 0, SEEK_SET);
  22. +
  23. idx = 0;
  24. while ((r = fread(buf, 1, sizeof(buf), fi)) > 0) {
  25. for (i = 0; i < r; i++) {
  26. - fprintf(fo, " 0x%.2x,", buf[i] & 0xff);
  27. + fprintf(fo, " 0x%.2x", buf[i] & 0xff);
  28. +
  29. + if(idx == file_size)
  30. + last_byte = 1;
  31. +
  32. + if (!last_byte) {
  33. + fprintf(fo, ",");
  34. + }
  35. +
  36. if (++idx % 8 == 0)
  37. fprintf(fo, "\n ");
  38. }
  39. @@ -81,6 +96,8 @@ int main(int argc, char **argv)
  40. fprintf(fo, " 0x%.2x", buf[i] & 0xff);
  41.  
  42. last_byte = feof(fi) && i == r-1;
  43. + if(idx == file_size)
  44. + last_byte = 1;
  45.  
  46. // dont print a comma after the last byte
  47. if (!last_byte) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement