Based on this answer about encoding/decoding to base 64, and this answer about converting from float to byte vector and back, I was trying to convert a float vector to a hex or binary string.
This is my code:
vector<float> floats = ...;
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&floats[0]);
std::vector<unsigned char> byteVec(bytes, bytes + sizeof(float) * floats.size());
std::cout<<byteVec.size()<<std::endl;
std::string dat = base64_encode(&byteVec[0], byteVec.size());
std::vector<unsigned char> decodedData = base64_decode(dat);
std::cout<<decodedData.size()<<std::endl;
unsigned char* bytesN = &(decodedData[0]); // point to beginning of memory
float* floatArray = reinterpret_cast<float*>(bytesN);
std::vector<float> floatVec(floatArray, floatArray + sizeof(unsigned char) * decodedData.size());
std::cout<<floatVec.size()<<std::endl;
The original vector, floats, is 262,144 floats, and both cout statements print 1048576, and then the program segfaults. What am I doing wrong?
Also, is there a better or faster method to accomplish this? I want to send the string using xml/gsoap, so if gsoap has a function to do this automatically, it would make my life a lot easier.
One major problem is this:
std::vector<float> floatVec(floatArray, floatArray + sizeof(unsigned char) * decodedData.size());
Here you say that the number of elements would be decodedData.size()
, which is 1048576
. But it's wrong since it should only be a quarter of that (because decodedData.size()
is the size in bytes not in the number of float
elements). This will of course lead to going out of bounds of decodedData
which leads to undefined behavior.
You need to divide the size of decodedData.size()
with sizeof(float)
:
std::vector<float> floatVec(floatArray, floatArray + decodedData.size() / sizeof(float));