TF++, Miscellaneous classes and modules in C++:

◆ hexdump() [1/2]

std::ostream & tfxx::util::hexdump ( const void *  pp,
const unsigned int &  size,
std::ostream &  os = std::cout,
const char &  c = '.',
const unsigned int &  n = 16 
)

output hex dump of memory area.

Parameters
[in]pmemory location to start dump at
[in]sizenumber of bytes to dump in sequence
[in]osoutput stream to which hex dump will be sent
[in]ccharacter to be used for non-printable characters
[in]nnumber of bytes per line
Returns
return stream os

Definition at line 44 of file hexdump.cc.

Referenced by hexdump(), and main().

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,
Here is the caller graph for this function: