TF++, Miscellaneous classes and modules in C++:
hexdump.cc
Go to the documentation of this file.
1 
35 #define TF_HEXDUMP_CC_VERSION \
36  "TF_HEXDUMP_CC V1.0"
37 
38 #include <tfxx/hexdump.h>
39 
40 namespace tfxx {
41 
42  namespace util {
43 
44  std::ostream& hexdump(const void* pp, const unsigned int& size,
45  std::ostream& os,
46  const char& c, const unsigned int&n)
47  {
48  // obtain pointer with desired type
49  const unsigned char* p=reinterpret_cast<const unsigned char*>(pp);
50  // character counter in object
51  unsigned short int ip=0;
52  // character counter in line
53  unsigned short int ic=0;
54  // character array for printable characters
55  char *pc=new char[n];
56 
57  // report size of object
58  os << "size of object: " << size << " bytes" << std::endl;
59 
60  // memorize output stream flags
61  std::ostream::fmtflags flags=os.flags();
62 
63  // cycle through all bytes in p
64  while (ip<size)
65  {
66  // start line with hex display of byte offset of first character
67  os << "0x";
68  os << std::hex << std::setfill('0') << std::setw(4)
69  << std::nouppercase << ip;
70 
71  // cycle through all characters in line
72  while ((ip < size) && (ic<n))
73  {
74  // extract numerical representation of current character
75  unsigned char ch=*p;
76  unsigned short int byte(ch);
77  // print hex value of current byte value
78  os << " ";
79  os << std::hex << std::setfill('0') << std::setw(2)
80  << std::nouppercase << byte;
81  os.flags(flags);
82  // store printable representation of character
83  pc[ic]=c;
84  if (std::isprint(ch)) { pc[ic]=ch; }
85  // step to next byte in object
86  ++p;
87  ++ip;
88  ++ic;
89  } // while ((ip < size) && (ic<n))
90 
91  // restore state flags of output stream
92  os.flags(flags);
93  // forward line columns to position of printable characters
94  for (unsigned int i=ic; i<n; i++) { os << " "; }
95  // output printable representation of bytes
96  os << " ";
97  for (unsigned int i=0; i<ic; i++) { os << pc[i]; }
98  // end line
99  os << std::endl;
100  ic=0;
101  } // while (ip<size)
102 
103  // delete temporary container of printable characters
104  delete[] pc;
105  return os;
106  } // std::ostream& hexdump(const char* p, const unsigned int& size,
107  // std::ostream& os=std::cout,
108  // const char& c='.', const unsigned int&n=16)
109 
110  } // namespace util
111 
112 } // namespace tfxx
113 
114 /* ----- END OF hexdump.cc ----- */
std::ostream & hexdump(const void *pp, const unsigned int &size, std::ostream &os, const char &c, const unsigned int &n)
output hex dump of memory area.
Definition: hexdump.cc:44
Namespace containing all code of library libtfxx.