写二进制数为bmp图像

问题描述:

我 后读入串矢量一个bmp图像和存储转换的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; 
} 

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

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?

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

ofstream saveFile(path, std::ios::binary); 

您不能使用格式化输出函数将数据写入二进制格式文件。使用saveFile.write()