I'm reading the bytes of a file (an image) to then convert into base64.
The bytes get written into a char vector called buffer, since I haven't found working examples for writing the bytes from the file into a char array
I do the char vector like such:
ifstream infile("image.png", ios_base::binary);
infile.seekg(0, ios_base::end);
size_t length = infile.tellg();
infile.seekg(0, ios_base::beg);
vector<char> buffer;
buffer.reserve(length);
copy(istreambuf_iterator<char>(infile),
istreambuf_iterator<char>(),
back_inserter(buffer)); infile.read(&buffer[0], length);
The base64 encoding function is:
int base64_encode(unsigned char *source, size_t sourcelen, char *target, size_t targetlen);
I need to send the base64 encoded text to a website, so that it can be displayed on a page for example.
In c++, vectors are dynamically allocated arrays. Whenever you call the .push_back()
method, the array is reallocated with the new data appended at the end of the array. If you really need to transfer the data from the vector into a regular array you could use a for loop to assign the data to the array like this:
for (int i = 0; i < vec.size() && i < arrLen; i++) {
arr[i] = vec[i];
}
Although a much better method considering vectors are just dynamically allocated arrays would be to transfer a raw pointer of the vector's first element to the function.
foo(&vec[0]);
OR
foo(vec.begin());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.