0

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;
}
2
  • What is the point of unsigned char* bytes = &(readRequestArray);? If you want to access the underlying array of a std::vector use the .data() method Commented Oct 10, 2018 at 13:11
  • 3
    Do note that C++ is hard and trying to learn everything you have been asking about would best be served by getting a good C++ book or two Commented Oct 10, 2018 at 13:12

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.