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. *
  16. * Motley Tools by Charles Maier;
  17. * Copyright (c) 2001-2006 by Charles Maier Associates;
  18. * Licensed under the Internet Software Consortium License;
  19. *
  20. *--------------------------------------------------------------------*/
  21. #ifndef BINOUT_SOURCE
  22. #define BINOUT_SOURCE
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "../tools/memory.h"
  26. #include "../tools/number.h"
  27. void binout (void const * memory, size_t extent, char c, char e, FILE * fp)
  28. {
  29. byte * offset = (byte *)(memory);
  30. while (extent--)
  31. {
  32. unsigned bits = 8;
  33. while (bits--)
  34. {
  35. putc (DIGITS_BIN [(* offset >> bits) & 1], fp);
  36. }
  37. if ((extent) && (c))
  38. {
  39. putc (c, fp);
  40. }
  41. offset++;
  42. }
  43. if (e)
  44. {
  45. putc (e, fp);
  46. }
  47. return;
  48. }
  49. #endif