简体   繁体   中英

Writing raw data to a file and using bin2c

Ok so I am fairly new to C++ and I came across a program the can take an exe or a bin file and put it in to a .h file for use with C++. The problem I am having is how can I write the raw data that bin2c genarats to a file and then reproduce that same exe. You can find the code below of the raw data. (Using cygwin to compile.) I was not on a PC so I was not able to use the code tags for showing the code sorry.

const unsigned char raw_data[] = {
    0x21, 0x20, 0xc2, 0xa6, 0xc3, 0xb4, 0xc2, 0xbf,
    0xc3, 0x82, 0xc3, 0x8b, 0xc2, 0xa4, 0x20, 0xc3,
    0x8d, 0xc3, 0x8c, 0x4c, 0x3f, 0x20, 0x20, 0xc3,
    0x80, 0xc3, 0xbf, 0x20, 0x20, 0xc3, 0x80, 0xc3,
    0xbf, 0xc2, 0xa5, 0x20, 0xc5, 0xa1, 0xe2, 0x84,
    0xa2, 0xc2, 0xa9, 0x40, 0x20, 0x20, 0xc3, 0x80,
    0xc3, 0xbf, 0x20, 0x20, 0xc3, 0x80, 0xc3, 0xbf,
    0x60, 0x20, 0x6f, 0x72, 0x20, 0x60, 0x21, 0x20,
    0x48, 0xc2, 0xb7, 0xc3, 0xb4, 0xc2, 0xbf, 0xc3,
    0x82, 0xc3, 0x8b, 0xc2, 0xa4, 0x20, 0xc3, 0x8d,
    0xc3, 0x8c, 0x4c, 0x3f, 0x20, 0x20, 0xc3, 0x80,
    0xc3, 0xbf, 0x33, 0x33, 0x33, 0x3f, 0xc2, 0xa5,
    0x20, 0xc5, 0xa1, 0xe2, 0x84, 0xa2, 0xc2, 0xa9,
    0x40, 0x20, 0x20, 0xc3, 0x80, 0xc3, 0xbf, 0x66,
    0x66, 0xc2, 0xa6, 0x40, 0x0a,
};

Use the standard C++ I/O gymnastics:

#include <fstream>

std::ofstream outfile("output.bin", std::ios::binary);

if (!outfile) { /* error, die! */ }

outfile.write(reinterpret_cast<char const *>(raw_data), sizeof raw_data);

You can just declare the array as a const char array and do away with the cast, if you prefer.

There is also my bin2c conversion tool that I made in C++ 14. Best of all it's open source and works on not only windows, but on OSX (MAC) and linux.

You can find it at https://www.github.com/AraHaan/bin2c <-- This link is for sure not to move unless github changes places and it is obvious that they should not.

The best part about my tool is that it is fast depending on the file size however at least it works. I do update it frequently when I find if there is bugs here and there but at least it gets the job done. I also have added a little compile time hack to it to where if the date on the month is less than 10 it strips one of the spaces in it to make the processed file look nice. The current version on that is v0.15.0 to. Also if you are wondering about why that 1 build fails on it it is because of travis not having clang 3.9 and g++ 5.3+. However I recommend building it either on g++ 6 or clang 3.9 (minimum version it handles because of experimental C++ 14 filesystem header).

Hopefully this helps solve for other people who wants a bin2c tool. Mine can also make files into C# arrays to for those people who want to do that as well.

Also my bin2c tool has an test that also provides an example and that example is as follows:

#include <string>
#include <iostream>
#include <fstream>
#include "VP.hpp"

int main()
{
    std::string created_msg = "Created ";
    created_msg += IMAGE_VP_NAME;
    created_msg += ".";
    std::string FILE_NAME = ".\\";
    FILE_NAME += IMAGE_VP_NAME;
    std::ofstream f(FILE_NAME, std::ios::out | std::ios::binary | std::ios::ate);
    f.write(reinterpret_cast<char const *>(IMAGE_VP), IMAGE_VP_SIZE);
    std::cout << created_msg << std::endl;
    std::cin.ignore();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM