How to convert float to vector in C++?

2019-09-01 13:23发布

问题:

I read Convert float vector to byte vector and back but it didn't help me to solve my issue. I want to convert std::vector<unsigned char> back to a float. The line on unsigned char* bytes = &(readRequestArray); is not working and the lines above I am only printing bytes. How do I convert back to a float?

class HCSR04: public ISensor {
public:
    HCSR04();
    HCSR04(int trigger, int echo);
    ~HCSR04();
    float distanceCentimeters();
    std::vector<unsigned char> readRequest();
}

std::vector<unsigned char> HCSR04::readRequest() {
    float preCent = distanceCentimeters();
    const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&preCent);
    std::vector<unsigned char> buffer(bytes, bytes + sizeof(float));
    for (int j = 0; j < buffer.size(); j++) {
        std::cout << buffer[j];
    }
    std::cout << std::endl;
    return buffer;
}

int main(void) {
    std::vector<unsigned char> readRequestArray = sensorUltrasonic->readRequest();
        for (int j = 0; j < readRequestArray.size(); j++) {
            std::cout << readRequestArray[j];
        }
        std::cout << std::endl;

        unsigned char* bytes = &(readRequestArray);
        for (int i = 0; i < 3; i++)
            std::cout << (float) bytes[i] << std::endl;
}

回答1:

To convert a float to and from a std::vector<unsigned char> you can use the following

auto to_vector(float f)
{
    // get vector of the right size
    std::vector<unsigned char> data(sizeof(f));
    // copy the bytes
    std::memcpy(data.data(), &f, sizeof(f));
    return data;
}

auto from_vector(const std::vector<unsigned char>& data)
{
    float f;
    // make sure the vector is the right size
    if (data.size() != sizeof(f))
        throw std::runtime_error{"Size of data in vector and float do not match"};
    // copy the bytes into the float
    std::memcpy(&f, data.data(), sizeof(f));
    return f;
}

int main()
{
    float foo = 3.14;
    auto data = to_vector(foo);
    auto ret = from_vector(data);
    std::cout << ret;
}


回答2:

Just copy it back to float:

float value;
std::copy( readRequestArray.begin(), readRequestArray.end(), reinterpret_cast<const unsigned char*>( &value ) );

Usually this is done by implementing functions that unmarshall from std::vector<byte> to various types for example:

 template<typename T>
 T read( std::vector<byte>::iterator &it )
 {
     auto prev = it;
     it += sizeof(T);
     T value;
     std::copy( prev, it, reinterpret_cast<byte *>( &value ) );
     return value;
 }

then use:

 std::vector<byte> data;
 auto it = data.begin();
 auto floatValue = read<float>( it );
 auto intValue   = read<int>( it );

but you need to be careful and use it only for POD types (maybe add std::enable_if to enforce it). Also you need to make sure that vector have enough data or pass a second iterator to the function to validate that there is enough data.



标签: c++ vector