I sending image encoded in Base64 on my c++ server as json value and trying to decode it back and save on disk as .jpg file. And i have a problem with decoding, its saves on disk but not opening, size of the input and output file slighly different (40.8 kb vs 41.1 kb). I tryed 3 different decoding code, this is the last one:
typedef unsigned char uchar; static const std::string b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//= std::string PictureManager::base64_decode(const std::string& in) { std::string out; std::vector<int> T(256, -1); for (int i = 0; i < 64; i++) T[b[i]] = i; int val = 0, valb = -8; for (uchar c : in) { if (T[c] == -1) break; val = (val << 6) + T[c]; valb += 6; if (valb >= 0) { out.push_back(char((val >> valb) & 0xFF)); valb -= 8; } } return out; } And this is my saving function :
bool PictureManager::SavePicture(std::string DecodedString) { std::ofstream Picture; Picture.open("PicturePatch.jpg"); if (!Picture.is_open()) { std::cout << "Error in opening file!" << std::endl; return false; } /*for(std::vector<BYTE>::iterator it = DecodedString.begin(); it != DecodedString.end(); it++) { Picture << *it; }*/ Picture << DecodedString; return true; } Please help
writemethod: Writing binary data to fstream in c++