67 lines
1.8 KiB
C++
67 lines
1.8 KiB
C++
//---------------------------------------------------------------------------//
|
|
|
|
#ifndef HEXSHOW
|
|
#define HEXSHOW
|
|
|
|
#include <sstream>
|
|
|
|
//---------------------------------------------------------------------------//
|
|
|
|
namespace hex
|
|
{
|
|
void show(std::vector<unsigned char> &mydata, unsigned long int width = 32,
|
|
unsigned long int startidx = 0,
|
|
unsigned long int breakidx = 512)
|
|
{
|
|
// check input
|
|
assert ( startidx <= breakidx );
|
|
breakidx = ( breakidx > mydata.size() || breakidx == 0 ) ? mydata.size() : breakidx;
|
|
|
|
// introduce additional line break
|
|
std::cout<<"\n";
|
|
|
|
// declare two stringstreams for hexadecimal and ASCII output
|
|
std::stringstream sshex, sschr;
|
|
|
|
// count bytes
|
|
unsigned long int idx = 0;
|
|
|
|
// process data
|
|
for ( std::size_t i = startidx; i < breakidx; i++ )
|
|
{
|
|
unsigned char el = mydata[i];
|
|
|
|
// hex representation
|
|
if ( (idx+0)%width == 0 )
|
|
{
|
|
sshex<<std::hex<<"0x"<<std::setw(6)<<std::setfill('0')<<idx<<": ";
|
|
}
|
|
sshex<<std::hex<<std::setw(2)<<std::setfill('0')<<(int)el;
|
|
sshex<<std::dec<<std::setfill(' ')<<" ";
|
|
|
|
// ascii representation
|
|
unsigned char elcl = ( (int)el > 0x1f && (int)el < 0x7f) ? el : '.';
|
|
sschr<<std::dec<<std::setfill(' ')<<elcl;
|
|
|
|
// do actual printing
|
|
if ( (idx+1)%width == 0 )
|
|
{
|
|
std::cout<<sshex.str()<<" "<<sschr.str();
|
|
std::cout<<"\n";
|
|
|
|
// clear stringstreams
|
|
sshex.str(std::string());
|
|
sschr.str(std::string());
|
|
}
|
|
|
|
// count number of bytes
|
|
idx++;
|
|
}
|
|
std::cout<<"\n";
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
//---------------------------------------------------------------------------//
|