如何将二进制数据推送到 std::string 中?

发布于 2024-11-29 14:53:17 字数 368 浏览 1 评论 0原文

我试图通过以下方式创建一个二进制文件:

string buf;
...
buf += filename.length();
buf += filename;

等等。所以首先我以二进制格式给出长度,但是如何将其转换为 4 字节字符数组或 2 字节等?基本上我想实现与此相同的功能:

int len = filename.length();
fwrite(&len, sizeof(len), 1, fp);

哪个工作正常,但将其放在一个字符串中可能更容易处理。

编辑:我不想使用流,也不想使用向量,我试图找出是否可以使用字符串。

Im trying to create a binary file in the following way:

string buf;
...
buf += filename.length();
buf += filename;

etc. So first i give the length in binary format, but how do i convert this into a 4 byte char array, or 2 byte etc? basically i want to achieve the same functionality as this would:

int len = filename.length();
fwrite(&len, sizeof(len), 1, fp);

Which works fine, but having it in one string might be easier to process.

Edit: i dont want to use streams, nor vector, im trying to find out if its possible with strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2024-12-06 14:53:17

流是必经之路。不是字符串

Streams are the way to go. Not strings.

寂寞美少年 2024-12-06 14:53:17

使用向量来保存数据,或直接将其写入文件(通过流)

Use a vector for holding the data, or write it straight to the file (via streams)

你对谁都笑 2024-12-06 14:53:17

只需使用 std:vector并使用 istream 或 ostream 迭代器从向量读取数据/向向量写入数据。例如,要从文件中读取,您可以执行以下操作:

vector<unsigned char> binary_buffer;

ifstream in_file("my_binary_file.bin", ios_base::binary | ios_base::in);
istream_iterator<unsigned char> end_of_file;
istream_iterator<unsigned char> in_file_iter(in_file);

while (in_file_iter != end_of_file)
{
    binary_buffer.push_back(*in_file_iter++);
}

输出会更简单:

ofstream out_file("another_binary_file.bin", ios_base::binary | ios_base::out);
ostream_iterator<unsigned char> binary_output(out_file);
copy(binary_buffer.begin(), binary_buffer.end(), binary_output);

simply use std:vector<unsigned char> and use a istream or ostream iterator to read/write data to/from the vector. For instance to read from a file you can do:

vector<unsigned char> binary_buffer;

ifstream in_file("my_binary_file.bin", ios_base::binary | ios_base::in);
istream_iterator<unsigned char> end_of_file;
istream_iterator<unsigned char> in_file_iter(in_file);

while (in_file_iter != end_of_file)
{
    binary_buffer.push_back(*in_file_iter++);
}

Output would be even simpler:

ofstream out_file("another_binary_file.bin", ios_base::binary | ios_base::out);
ostream_iterator<unsigned char> binary_output(out_file);
copy(binary_buffer.begin(), binary_buffer.end(), binary_output);
寻找我们的幸福 2024-12-06 14:53:17

是的,这是可以做到的,因为这是C++,在C++中一切皆有可能。

首先,这是你如何做到这一点,然后我将回答你为什么可能需要这样做的问题:

std::ifstream input( "my.png", std::ios::binary );
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
int buffSize = buffer.size();
std::string myString(buffer.begin(), buffer.end());

就我而言,我使用的是一个框架,其中HTTP 客户端仅支持用于发布消息正文的字符串,但我需要将文件的原始二进制文件发布到我正在使用的服务。我可能会搞乱库的内部结构(不好),或者为简单的文件发布引入另一个不必要的依赖项(也不太好)。

但由于字符串可以保存这些数据,因此我能够读取该文件,将其复制到字符串中,然后上传。

无论哪种方式,这都是一种不幸的情况,但在某些情况下,能够做到这一点是有帮助的。通常你会想做一些不同的事情。对于其他人来说,这并不是最清晰的阅读方式,并且您会因副本而受到一些性能损失,但它确实有效;它有助于理解其工作原理的内部原理。 std::string 在内部以二进制格式连续保存数据,就像向量一样,并且可以从向量中的迭代器进行初始化。

unsigned char 是一个字节长,因此它就像一个 byte 值。该字符串将字节复制到自身中,因此您最终会得到准确的数据。

Yes, it is possible to do this, because this is C++, and everything is possible in C++.

First, here is how you do it, then I'll answer the question of why you might need to do it:

std::ifstream input( "my.png", std::ios::binary );
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
int buffSize = buffer.size();
std::string myString(buffer.begin(), buffer.end());

In my case, I was using a framework where the HTTP Client only supported a string for post message body, but I needed to post raw binary of a file to the service I was using. I could either mess around with the internals of the library (bad) or introduce another unnecessary dependency for a simple file post (also not so good).

But since strings can hold this data, I was able to read the file, copy it to a string and then upload.

This is an unfortunate situation to be in, either way, but in some cases it is helpful to be able to do. Usually you would want to do something different. This isn't the clearest for someone else to read, and you will have some performance penalties from the copy, but it works; and it helps to understand the internals of why it works. std::string saves the data contiguously internally in binary format, like vector does, and can be initialized from iterators in the vector.

unsigned char is one byte long, so it is like a byte value. The string copies the bytes into itself, so you end up with the exact data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文