开发者

Need Convert Binary file to Txt file

开发者 https://www.devze.com 2023-02-14 14:04 出处:网络
I have a dat(binary) file but i wish to convert this file into Ascii (txt) file using c++ but i am very new in c++ programming.so I juct opend my 2 files:myBinaryfile and myTxtFile but I don\'t know h

I have a dat(binary) file but i wish to convert this file into Ascii (txt) file using c++ but i am very new in c++ programming.so I juct opend my 2 files:myBinaryfile and myTxtFile but I don't know how to read data from that dat file and then how to write those data into new txt file.so i want to write a c+ codes that takes in an input containing binary dat file, and converts it to Ascii txt in an output file. if this possible please help to write this codes. thanks

Sorry for asking same question again but still I didn’t solve my problem, I will explain it more clearly as fo开发者_高级运维llows: I have a txt file called “A.txt”, so I want to convert this into binary file (B.dat) and vice verse process. Two questions: 1. how to convert “A.txt” into “B.dat” in c++ 2. how to convert “B.dat” into “C.txt” in c++ (need convert result of the 1st output again into new ascii file)

my text file is like (no header):

1st line: 1234.123 543.213 67543.210 1234.67 12.000 2nd line: 4234.423 843.200 60543.232 5634.60 72.012

it have more than 1000 lines in similar style (5 columns per one line).

Since I don’t have experiences in c++, I am struggle here, so need your helps. Many Thanks


All files are just a stream of bytes. You can open files in binary mode, or text mode. The later simply means that it may have extra newline handling.

If you want your text file to contain only safe human readable characters you could do something like base64 encode your binary data before saving it in the text file.


Very easy:

  1. Create target or destination file (a.k.a. open).
  2. Open source file in binary mode, which prevents OS from translating the content.
  3. Read an octet (byte) from source file; unsigned char is a good variable type for this.
  4. Write the octet to the destination using your favorite conversion, hex, decimal, etc.
  5. Repeat at 3 until the read fails.
  6. Close all files.

Research these keywords: ifstream, ofstream, hex modifier, dec modifier, istream::read, ostream::write.

There are utilities and applications that already perform this operation. On the *nix and Cygwin side try od, *octal dump` and pipe the contents to a file.

There is the debug utility on MS-DOS system.

A popular format is:

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.

Please edit your post to provide more details, including an example layout for the target file.

Edit:

A complex example (not tested):

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
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 << "Error opening input file." << endl;
            break;
        }
        ofstream dest_file("binary.txt");
        if (!dest_file)
        {
            cerr << "Error creating output file." << endl;
        }

        // While no read errors from reading a block of source data:
        while (srce_file.read(&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 < 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 >> 8)];

                // Convert the Least Significant nibble to an
                //   ASCII character and put into output buffer.  
                write_buffer[i * 2 + 1] = hex_chars[byte & 0x0f];
            }

            // Write the output buffer to the output, text, file.
            dest_file.write(&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;
}

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.


I think you are misunderstanding that the difference between a binary file and a test file is in the interpretation of the contents.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号