Created
September 17, 2016 10:23
-
-
Save MrSapps/cbd037b3d1b063927b719e489197aa27 to your computer and use it in GitHub Desktop.
HexPrint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <windows.h> | |
#include <iomanip> | |
#include <iostream> | |
#include <sstream> | |
#include <cmath> | |
static std::string ByteToHex(unsigned char data) | |
{ | |
unsigned int convertedData = (unsigned int)(data); | |
std::stringstream ss; | |
ss << std::hex; | |
ss << std::setfill('0'); | |
ss << std::setw(2); | |
ss << convertedData; | |
return ss.str(); | |
} | |
static char ByteToAscii(unsigned char data) | |
{ | |
if ( isalpha(data) || isdigit(data) ) | |
{ | |
return data; | |
} | |
return '.'; | |
} | |
static void hex_print(const unsigned char* start, const unsigned char* end) | |
{ | |
const ptrdiff_t memSizeBytes = std::abs( start - end ); | |
const size_t bytePerRow = 44; | |
size_t numRows = memSizeBytes / bytePerRow; | |
if ( memSizeBytes % bytePerRow ) | |
{ | |
numRows++; | |
} | |
std::stringstream line; | |
ptrdiff_t pos = 0; | |
for ( size_t i=0; i<numRows; i++ ) | |
{ | |
ptrdiff_t oldPos = pos; | |
std::stringstream addr; | |
addr << "0x"; | |
addr << std::hex << std::setfill('0'); | |
addr << std::setw(4) << (unsigned int)(start+pos); | |
line << addr.str().c_str() << " "; | |
for ( size_t j=0; j<bytePerRow; j++ ) | |
{ | |
if ( pos < memSizeBytes ) | |
{ | |
line << ByteToHex(start[pos]); | |
line << " "; | |
pos++; | |
} | |
else | |
{ | |
line << "XX"; | |
line << " "; | |
} | |
} | |
pos = oldPos; | |
for ( size_t j=0; j<bytePerRow; j++ ) | |
{ | |
if ( pos < memSizeBytes ) | |
{ | |
line << char(ByteToAscii(start[pos])); | |
pos++; | |
} | |
else | |
{ | |
line << '?'; | |
} | |
} | |
line << "\n"; | |
} | |
line << "\n"; | |
OutputDebugStringA(line.str().c_str()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment