简体   繁体   English

将原始数据写入文件并使用bin2c

[英]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++. 好的,所以我对C ++还是相当陌生,我遇到了一个程序,该程序可以将exe或bin文件放入一个.h文件中,以便与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. 我遇到的问题是如何将bin2c genarats的原始数据写入文件,然后重现该相同的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. (使用cygwin进行编译。)我不在PC上,因此无法使用代码标签显示对不起的代码。

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: 使用标准的C ++ I / O体操:

#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. 您可以将数组声明为const char数组,并根据需要取消强制转换。

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. 还有我用C ++ 14制作的bin2c转换工具。最棒的是,它是开源的,不仅可以在Windows上使用,而且可以在OSX(MAC)和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. 您可以在https://www.github.com/AraHaan/bin2c上找到它---除非github更改了位置,而且显然不应该更改,否则此链接肯定不会移动。

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. 我还向它添加了一些编译时技巧,如果该月的日期小于10,它将删除其中的一个空格以使处理后的文件看起来不错。 The current version on that is v0.15.0 to. 该版本的当前版本为v0.15.0。 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+. 另外,如果您想知道为什么1的构建失败,那是因为travis没有clang 3.9和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). 但是,我建议在g ++ 6或clang 3.9上构建它(由于实验性的C ++ 14文件系统标头,它可以处理的最低版本)。

Hopefully this helps solve for other people who wants a bin2c tool. 希望这有助于解决其他需要bin2c工具的人。 Mine can also make files into C# arrays to for those people who want to do that as well. 我的人也可以将文件制作成C#数组,以供那些也想这样做的人使用。

Also my bin2c tool has an test that also provides an example and that example is as follows: 我的bin2c工具也有一个测试,该测试也提供了一个示例,该示例如下:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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