Skip to content

Instantly share code, notes, and snippets.

@coreyoconnor
Created November 13, 2008 22:11
Show Gist options
  • Save coreyoconnor/24651 to your computer and use it in GitHub Desktop.
Save coreyoconnor/24651 to your computer and use it in GitHub Desktop.
A quick hack to make a hex dumper for boost::arrays
template<typename array_type, std::size_t array_size>
std::string hex_dump_array(const boost::array<array_type, array_size>& in_array)
{
// 100 chars max stride. 2 chars per byte.
using namespace std;
const uint32_t chunks_per_stride = 50 / (sizeof(array_type) + 1);
uint32_t chunk_offset = 0;
std::stringstream out_stream;
std::string stride_txt;
for(typename boost::array<array_type, array_size>::const_iterator i = in_array.begin();
i != in_array.end();
++i, ++chunk_offset)
{
if(chunk_offset % chunks_per_stride == 0)
{
if(chunk_offset != 0)
{
out_stream << " " << stride_txt;
stride_txt.clear();
out_stream << "\n";
}
out_stream << "0x" << setw(4) << setfill('0') << hex << chunk_offset;
}
out_stream << " " << setw(sizeof(array_type) * 2) << setfill('0') << hex << (uint64_t)*i;
if(isgraph(*i)) // Not valid for all array_types!!
stride_txt += string(1, *i);
else
stride_txt += "X";
}
out_stream << " " << stride_txt;
out_stream << "\n";
return out_stream.str();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment