hex_dump.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* hex_dump.h -- simple hex dump routine
  2. *
  3. * Copyright (C) 2002 convergence GmbH
  4. * Johannes Stezenbach <js@convergence.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 2.1
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "hex_dump.h"
  23. void hex_dump(uint8_t data[], int bytes)
  24. {
  25. int i, j;
  26. uint8_t c;
  27. for (i = 0; i < bytes; i++) {
  28. if (!(i % 8) && i)
  29. printf(" ");
  30. if (!(i % 16) && i) {
  31. printf(" ");
  32. for (j = 0; j < 16; j++) {
  33. c = data[i+j-16];
  34. if ((c < 0x20) || (c >= 0x7f))
  35. c = '.';
  36. printf("%c", c);
  37. }
  38. printf("\n");
  39. }
  40. printf("%.2x ", data[i]);
  41. }
  42. j = (bytes % 16);
  43. j = (j != 0 ? j : 16);
  44. for (i = j; i < 16; i++) {
  45. if (!(i % 8) && i)
  46. printf(" ");
  47. printf(" ");
  48. }
  49. printf(" ");
  50. for (i = bytes - j; i < bytes; i++) {
  51. c = data[i];
  52. if ((c < 0x20) || (c >= 0x7f))
  53. c = '.';
  54. printf("%c", c);
  55. }
  56. printf("\n");
  57. }