i have tried to write this hexdump code for dumping the contents of a tex file.

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{  
  string line;
  string finline;
  char infile[100];
  char ofile[100];
  char chars[750];

  cout<<"Enter input file: ";
  cin.ignore();
  cin.getline(infile, 99);
  cout<<"Enter output file: ";
  cin.getline(ofile, 99);

  ofstream out;
  out.open(ofile);

  ifstream file(infile);

  while(getline(file, line))
  {
    finline += line;
    for(int i = 0; i < finline.size(); i++)
    {
      chars[i] = finline[i];
    }
  }

  for(int i = 0; i < finline.size(); i++)
  {
    chars[i] = finline[i];
  }

  out<<hex<<(int)chars;
  cout<<hex<<(int)chars<<endl;

  return 0;
}

but it only give 1 line eg. 0b3d6fc9
and not as much as i think it should.

the file contains 3 lines of data, cant think that 3 lines can be in that 1 little hex line.

Anyway please help me write 1 that will work for dumping contents of files and maby even system memory, the files one is more important at the moment.

Thanks....

Dani AI

Generated

Quick diagnosis and what went wrong
was printing (int)chars, which converts the array to a pointer and writes the pointer value (one hex number), not the bytes. Reading with getline (text mode) also removes line endings and is the wrong approach for arbitrary bytes. was right to say “iterate the buffer,” but you must cast each byte to an integer type and format it (operator<< prints char as a character, not a numeric hex byte).

A compact, robust file hexdump (16 bytes/line)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <cctype>
#include <string>

int main(int argc, char* argv[])
{
    std::string name = (argc > 1) ? argv[1] : "input.bin";
    std::ifstream in(name.c_str(), std::ios::binary);
    if (!in) { std::cerr << "Cannot open: " << name << "\n"; return 1; }

    const std::size_t COLS = 16;
    std::vector<unsigned char> buf(COLS);
    std::size_t offset = 0;

    while (in) {
        in.read(reinterpret_cast<char*>(&buf[0]), buf.size());
        std::streamsize n = in.gcount();
        if (n <= 0) break;

        std::cout << std::hex << std::setw(8) << std::setfill('0') << offset << ": ";

        for (std::streamsize i = 0; i < COLS; ++i) {
            if (i < n)
                std::cout << std::hex << std::setw(2) << std::setfill('0')
                          << static_cast<int>(buf[i]);
            else
                std::cout << "  ";
            if (i % 2) std::cout << ' ';
        }

        std::cout << " ";
        for (std::streamsize i = 0; i < n; ++i) {
            unsigned char c = buf[i];
            std::cout << (std::isprint(c) ? static_cast<char>(c) : '.');
        }
        std::cout << "\n";

        offset += n;
    }
    return 0;
}

Quick tips and cautions

  • Open files with std::ios::binary and use read/gcount for raw bytes.
  • Cast bytes to an unsigned integer when printing hex: static_cast<int>(unsigned_char); printing char directly shows characters.
  • Use a fixed-width column (16 is common) and print an ASCII column for readability.
  • Dumping system memory is OS-specific and requires privileges; it is outside the scope of this file-focused approach.

Recommended Answers

All 2 Replies

You need to loop the chars array. Something like:

for (int i = 0; i < finline.size(); ++i) {
   cout << hex << chars[i];
}

Still doesnt work, now it doesnt want to print anything

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.