binout.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*====================================================================*
  2. *
  3. * void binout (void const * memory, size_t extent, char c, char e, FILE * fp)
  4. *
  5. * memory.h
  6. *
  7. * print memory as a series of binary bytes separated by character
  8. * c; normally, character c will be BIN_EXTENDER as defined
  9. * in number.h;
  10. *
  11. * for example, binout (memory, 6, '-', stdout) would print
  12. *
  13. * 00000000-11010000-01010010-00000000-00000000-00000001
  14. *
  15. * Motley Tools by Charles Maier <cmaier@cmassoc.net>;
  16. * Copyright (c) 2001-2006 by Charles Maier Associates;
  17. * Licensed under the Internet Software Consortium License;
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef BINOUT_SOURCE
  21. #define BINOUT_SOURCE
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include "../tools/memory.h"
  25. #include "../tools/number.h"
  26. void binout (void const * memory, size_t extent, char c, char e, FILE * fp)
  27. {
  28. byte * offset = (byte *) (memory);
  29. while (extent--)
  30. {
  31. unsigned bits = 8;
  32. while (bits--)
  33. {
  34. putc (DIGITS_BIN [(* offset >> bits) & 1], fp);
  35. }
  36. if ((extent) && (c))
  37. {
  38. putc (c, fp);
  39. }
  40. offset++;
  41. }
  42. if (e)
  43. {
  44. putc (e, fp);
  45. }
  46. return;
  47. }
  48. #endif