The company I'm working for has asked me to find a way to convert an image into Base64. Basically, there is a camera that will be taking pictures in JPG and I need to convert that JPG picture in Base64 so that I can send the data through a PLC program and rebuild it on the application side which is going to be a web app.
I will then have to do :
document.getElementById("ImageLoad").src = "data:image/png;base64," + Bytes;
In Javascript and do the Jquery.
I've tried using the ifstream with ios::in | ios::binary and just reading the file and outputting the result but it doesn't work.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream input("test.jpg", ios::in | ios::binary);
ofstream output("text.txt");
if (input.is_open()) {
while (getline(input,line)) {
output << line;
}
input.close();
}
}
I'm expecting an output like the following:
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBAQFBA
But I'm getting a long string looking like this:
}!× ÙÝÜ÷åXŠmŒš@Õä ‡6gxD1;*wïµ¼4 ÒÑôÿ ¿OÑú\x0¥ˆ‘ÀÃõûóC