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.