Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Very easy:</p> <ol> <li>Create target or destination file (a.k.a. open).</li> <li>Open source file in <code>binary</code> mode, which prevents OS from translating the content.</li> <li>Read an octet (byte) from source file; <code>unsigned char</code> is a good variable type for this.</li> <li>Write the octet to the destination using your favorite conversion, hex, decimal, etc.</li> <li>Repeat at 3 until the read fails.</li> <li>Close all files.</li> </ol> <p>Research these keywords: <code>ifstream, ofstream, hex modifier, dec modifier, istream::read, ostream::write</code>. </p> <p>There are utilities and applications that already perform this operation. On the *nix and Cygwin side try <code>od</code>, *octal dump` and pipe the contents to a file.</p> <p>There is the <code>debug</code> utility on MS-DOS system. </p> <p>A popular format is: </p> <pre><code>AAAAAA bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb cccccccccccccccc where: AAAAAA -- Offset from beginning of file in hexadecimal or decimal. bb -- Hex value of byte using ASCII text. c -- Character representation of byte, '.' if the value is not printable. </code></pre> <p>Please edit your post to provide more details, including an example layout for the target file.</p> <h2>Edit:</h2> <p>A complex example (not tested): </p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; using namespace std; const unsigned int READ_BUFFER_SIZE = 1024 * 1024; const unsigned int WRITE_BUFFER_SIZE = 2 * READ_BUFFER_SIZE; unsigned char read_buffer[READ_BUFFER_SIZE]; unsigned char write_buffer[WRITE_BUFFER_SIZE]; int main(void) { int program_status = EXIT_FAILURE; static const char hex_chars[] = "0123456789ABCDEF"; do { ifstream srce_file("binary.dat", ios::binary); if (!srce_file) { cerr &lt;&lt; "Error opening input file." &lt;&lt; endl; break; } ofstream dest_file("binary.txt"); if (!dest_file) { cerr &lt;&lt; "Error creating output file." &lt;&lt; endl; } // While no read errors from reading a block of source data: while (srce_file.read(&amp;read_buffer[0], READ_BUFFER_SIZE)) { // Get the number of bytes actually read. const unsigned int bytes_read = srce_file.gcount(); // Define the index and byte variables outside // of the loop to maybe save some execution time. unsigned int i = 0; unsigned char byte = 0; // For each byte that was read: for (i = 0; i &lt; bytes_read; ++i) { // Get source, binary value. byte = read_buffer[i]; // Convert the Most Significant nibble to an // ASCII character using a lookup table. // Write the character into the output buffer. write_buffer[i * 2 + 0] = hex_chars[(byte &gt;&gt; 8)]; // Convert the Least Significant nibble to an // ASCII character and put into output buffer. write_buffer[i * 2 + 1] = hex_chars[byte &amp; 0x0f]; } // Write the output buffer to the output, text, file. dest_file.write(&amp;write_buffer[0], 2 * bytes_read); // Flush the contents of the stream buffer as a precaution. dest_file.flush(); } dest_file.flush(); dest_file.close(); srce_file.close(); program_status = EXIT_SUCCESS; } while (false); return program_status; } </code></pre> <p>The above program reads 1MB chunks from the binary file, converts to ASCII hex into an output buffer, then writes the chunk to the text file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload