0

在将 char 转换为数字和二进制数后,我读取了 bmp 图像并存储在字符串向量中:

typedef unsigned char BYTE;
std::streampos fileSize;
std::vector<BYTE> readFile(const char* filename)
{
    // open the file:

    std::ifstream file(filename, std::ios::binary);

    // get its size:
    file.seekg(0, std::ios::end);
    fileSize = file.tellg();
    file.seekg(0, std::ios::beg);

    // read the data:
    std::vector<BYTE> fileData(fileSize);
    file.read((char*) &fileData[0], fileSize);
    return fileData;
}

现在这很好,但是我想在将每个二进制数转换为 char 并将其存储在新文件中之后重写 bmp 文件。

ofstream saveFile(path);
int i=0; string str="";
while(i<binary.size())  //the binary_size is a string that contain all binary number of bmp
    {
    str=BinartToInt(binary[i]);//BinartToInt is a function that convert 8bit binary to number
    saveFile <<str;
    i++;
    }

saveFile.close();

如何将矢量二进制字符串转换为 BMP?

4

2 回答 2

0

确保您的输出流也标记为二进制,否则行尾和其他文本处理将对您的二进制流生效。

ofstream saveFile(path, std::ios::binary);
于 2013-03-20T17:39:16.170 回答
0

You cannot use formatted output functions to write data to binary format files. Use saveFile.write().

于 2013-03-20T17:52:28.763 回答