/* * HexDump * (c) Markus Lumpe, V1.0 * */ #include #include #include using namespace std; int main( int argc, char *argv[] ) { // check number of arguments if ( argc < 2 ) { cerr << "Missing argument." << endl; return 1; } // open streams ifstream lReader; ofstream lOutput; lReader.open( argv[1], ifstream::binary ); if ( lReader.fail() ) { cerr << "Cannot open file \'" << argv[1] << "\'." << endl; return 1; } string lOutName( argv[1] ); lOutName += ".txt"; lOutput.open( lOutName.c_str() ); if ( lOutput.fail() ) { cerr << "Cannot open output file \'" << lOutName.c_str() << "\'." << endl; return 1; } lOutput << "Hex dump of \'" << argv[1] << "\'" << endl << endl; //---- write hex dump -------------------------------------------// unsigned long lCount = 0L; unsigned char lBytes[17]; lOutput << uppercase; // looks better lOutput << hex; // force hexadecimal for ( ; ; ) { // read next 16 bytes; lReader.read( (char *)lBytes, 16 ); int lRead = lReader.gcount(); if ( lRead == 0 ) { break; } lOutput << setw(8) << setfill('0') << lCount << ": "; for ( int i = 0; i < lRead; i++ ) { lOutput << " " << setw(2) << setfill('0') << (int)lBytes[i]; if ( i == 8 ) { lOutput << " |"; } } // padding if ( lRead < 9 ) { cout << " "; } for ( int i = lRead; i < 16; i++ ) { lOutput << " "; } // original text (converted to printable characters) for ( int i = 0; i < lRead; i++ ) { if ( (lBytes[i] < 32) || (lBytes[i] > 126) ) { lBytes[i] = (unsigned char)'.'; } } lBytes[lRead] = (unsigned char)'\0'; lOutput << " " << (char *)lBytes << endl; lCount += 16L; if ( lRead < 16 ) { break; } } //----- done ----------------------------------------------------// lReader.close(); lOutput.close(); return 0; }