如何保存重新编码的数据? [英] How do I save recoded data?

查看:75
本文介绍了如何保存重新编码的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将接收到的重新编码的数据保存到另一个文件中,以便可以将其解码回去.

I want to save the received recoded data to another file so that it can be decoded back.

但是现在我想将jpg图像编码为相同的jpg格式,而无需更改ascii代码

But for now I want to encode a jpg image to the same jpg format without changing the ascii code

因此,即使通过编码传递并单独保存,我也希望获得相同的图像.

As a result, I want to get the same image even though it was passed through encoding and saved separately.

这是我的代码:

static constexpr int64_t ascii_encoding[] {
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
    31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 , 60,
    61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
    91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
    121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150,
    151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
    181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
    211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
    241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
};


std::filesystem::path filename = "test.jpg";
std::ifstream in(filename, std::ios::binary);


for (unsigned char c; in.get((char&)c);) {

    std::cout << (int)ascii_encoding[c] << " | " << c << " | " << (int)c << std::endl
}

还必须将数据存储在 255 上方以进行进一步解码.例如,我可以将ASCII码 104 更改为 777 并保存.

It is also necessary that the data can be stored above 255 for further decoding. For example, I can change ASCII number 104 to 777 and save.

推荐答案

因此,我不清楚在清楚地具有文件输入操作的情况下为什么无法弄清楚文件输出操作的原因.但是无论如何,这里是代码

So I'm not clear why you can't figure out the file output operations when you clearly have the file input operations. But anyway here is the code

std::ifstream in("test.jpg", std::ios::binary);
std::ofstream out("test.jpg.out", std::ios::binary);
for (unsigned char c; in.get((char&)c);) {
    std::cout << (int)ascii_encoding[c] << " | " << c << " | " << (int)c << std::endl;
    out.put((char)ascii_encoding[c]);
}

就是这样.一行代码打开输出文件,一行代码输出编码后的值.

That's it. One line of code to open the output file, and one line of code to output the encoded value.

编辑

似乎您想将 int64_t 值输出到文件中.可以按照以下步骤完成

It seems that you want to output int64_t values to your file. That can be done as follows

    out.write((char*)&ascii_encoding[c], sizeof ascii_encoding[c]);

请注意,此代码是特定于平台的,如果在一个平台上使用此代码,则可能难以在另一个平台上读回代码.

Be aware that this code is platform specific, if you use this code on one platform you might have trouble reading the codes back on a different platform.

这篇关于如何保存重新编码的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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