2013-03-20 110 views
0

我 後讀入串矢量一個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?

回答

0

確保您的輸出流也被標記爲二進制,否則行結束和其他文本處理將在您的二進制流中生效。

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

您不能使用格式化輸出函數將數據寫入二進制格式文件。使用saveFile.write()