[英]How to convert float to vector<unsigned char> in C++?
I read Convert float vector to byte vector and back but it didn't help me to solve my issue. 我读了将float向量转换为byte向量并返回的内容,但是它没有帮助我解决问题。 I want to convert
std::vector<unsigned char>
back to a float
. 我想将
std::vector<unsigned char>
转换回float
。 The line on unsigned char* bytes = &(readRequestArray);
unsigned char* bytes = &(readRequestArray);
上的行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;
}
To convert a float
to and from a std::vector<unsigned char>
you can use the following 要将
float
与std::vector<unsigned char>
进行std::vector<unsigned char>
转换,可以使用以下命令
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;
}
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: 通常,这是通过实现从
std::vector<byte>
解构为各种类型的函数来完成的,例如:
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). 但您需要注意并仅将其用于POD类型(也许添加
std::enable_if
来强制实施)。 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. 另外,您还需要确保向量具有足够的数据,或者将第二个迭代器传递给函数以验证是否有足够的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.