C ++:如何使用Visual Studio将原始二进制数据添加到源中? [英] C++: How to add raw binary data into source with Visual Studio?

查看:101
本文介绍了C ++:如何使用Visual Studio将原始二进制数据添加到源中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件,我想直接嵌入到我的源代码中,因此它将直接编译到.exe文件中,而不是从文件中读取它,因此当我将数据存储在内存中时,启动程序.

I have a binary file which i want to embed directly into my source code, so it will be compiled into the .exe file directly, instead of reading it from a file, so the data would already be in the memory when i launch the program.

我该怎么做?

我唯一的想法是将二进制数据编码为base64,将其放入字符串变量中,然后将其解码回原始二进制数据,但这是棘手的方法,这将导致无意义的内存分配.另外,我想像原始数据一样紧凑地将数据存储在.exe中.

Only idea i got was to encode my binary data into base64, put it in a string variable and then decode it back to raw binary data, but this is tricky method which will cause pointless memory allocating. Also, i would like to store the data in the .exe as compact as the original data was.

编辑:之所以想到使用base64,是因为我也想使源代码文件尽可能小.

The reason i thought of using base64 was because i wanted to make the source code files as small as possible too.

推荐答案

最简单,最可移植的方法是编写一小段 该程序将数据转换为C ++源代码,然后进行编译 并将其链接到您的程序中.此生成的文件可能 看起来像这样:

The easiest and most portable way would be to write a small program which converts the data to a C++ source, then compile that and link it into your program. This generated file might look something like:

unsigned char rawData[] =
{
    0x12, 0x34, // ...
};

这篇关于C ++:如何使用Visual Studio将原始二进制数据添加到源中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆