I'm sending a fixed length byte stream over a serial port to another computer and I'd like to try and convert the byte stream to a vector of floating point numbers.
My stream has a delimiter stop
and I'm using this serial library.
My current implementation involves the following:
I read a string
std::string data_string;
ser.readline(data_string, 2052, "stop");
Check if the string ends with the delimiter
if(boost::algorithm::ends_with(stop_string, "stop"))
{
if(data_string.length() == 2052)
Then convert the string to a vector
std::vector<unsigned char>bytes(stop_string.begin(), stop_string.end());
I then use a for loop and memcpy
to convert bytes
to an array of floating point numbers.
unsigned char temp_buffer[4];
float float_values[513] = { 0 };
int j = 0;
for(size_t i = 4; i < 2052; i+=4)
{
temp_buffer[0] = bytes[i - 4];
temp_buffer[1] = bytes[i - 3];
temp_buffer[2] = bytes[i - 2];
temp_buffer[3] = bytes[i - 1];
memcpy(&float_values[j], temp_buffer, sizeof(float));
j++;
}
But this method looks cumbersome and I'd like to avoid the for loop. Is there a way to:
convert the
bytes
vector to a vector of floating point numbers instead?avoid the for loop?