/****************************************************************************** * This program reads bytes from a file and prints them in hexadecimal format. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ #undef NDEBUG #include "Utils.hpp" #include #include #include #include using namespace Utils; using namespace std; void read_binary_file(string filespec) { ifstream ifh(filesystem::path(filespec).c_str(), ios_base::binary); if (!ifh.good()) { throw ios_base::failure("Problem opening input file!"); } unsigned char c; int count = 0; while (Utils::getbytes(ifh, c)) { cout << format("{0:02x} ", int32_t(c)); ++count; if (count % 16 == 0) { cout << endl; } } if (count % 16 != 0) { cout << endl; } ifh.close(); } int main(int argc, char **argv) { if (argc != 2) { cout << "Syntax: " << argv[0] << " {filename}" << endl; exit(1); } string const FILESPEC = string(argv[1]); try { read_binary_file(FILESPEC); } catch (ios_base::failure ex) { cout << "Error: " << Utils::exceptionMessage(ex) << endl; } catch (...) { cout << "Unexpected File Read Error" << endl; } return 0; }