How do I save recoded data?

拜拜、爱过 提交于 2021-01-07 03:14:05

问题


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

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.

here's my code:

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
}

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.


回答1:


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.

EDIT

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.




回答2:


I find fread and fwrite much more intuitive when dealing with binary files. The following code takes a png file (format doesn't matter though. You may remove the extension and it will still work.) & makes a copy of it.

If the original data doesn't have anything beyond the limit of 255 that you say, this code won't modify the copy at all.

Though TOTAL and buf_size can be handled in a better way.

#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>

const int TOTAL = 58393 * 4;
// Keep buffer a multiple of TOTAL.
const int buf_size = TOTAL / 4;
const std::string infilepath = "png.png";
const std::string outfilepath = "pngcopy.png";

#define FOR_ALL_OF_THEM for (int i = 0; i < TOTAL; ++i)

void read_bin(std::vector<int> &all_of_them)
{
  FILE *infile = fopen(infilepath.c_str(), "rb");
  FOR_ALL_OF_THEM
  {
    fread(&all_of_them[i], sizeof(int), buf_size, infile);
    i += buf_size - 1;
  }
}

void write_bin(const std::vector<int> &all_of_them)
{
  FILE *outfile = fopen(outfilepath.c_str(), "wb");
  FOR_ALL_OF_THEM
  {
    fwrite(&all_of_them[i], sizeof(int), buf_size, outfile);
    i += buf_size - 1;
  }
  fclose(outfile);
}

int main()
{
  std::vector<int> all_of_them(TOTAL);
  read_bin(all_of_them);
  write_bin(all_of_them);
}



来源:https://stackoverflow.com/questions/63352544/how-do-i-save-recoded-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!