Examples/hexdump.cxx

46 lines
1.0 KiB
C++

#include <iostream>
#include <fstream>
#include <iomanip>
#include <inttypes.h>
#include <sstream>
#include <string>
#include <algorithm>
std::string convert_ASCII(std::string hex){
std::string ascii = "";
for (size_t i = 0; i < hex.length(); i += 2){
//taking two characters from hex string
std::string part = hex.substr(i, 2);
//changing it into base 16
char ch = stoul(part, nullptr, 16);
//putting it into the ASCII string
ascii += ch;
}
reverse(ascii.begin(), ascii.end());
return ascii;
}
int main(int argc, char** argv){
std::string filename = argv[1];
std::ifstream in(filename, std::ios::binary);
while(!in.eof()){
uint32_t word;
in.read((char*) &word, sizeof(word));
std::stringstream ss;
ss << std::hex << word;
std::string str;
ss >> str;
std::cout << "0x" << std::hex
<< std::setw(8) << std::setfill('0')
<< word << "\t" << convert_ASCII(str) << std::endl;
}
in.close();
return 0;
}