esp32-car/src/utils.cpp

31 lines
401 B
C++
Raw Normal View History

2024-12-27 09:47:56 +08:00
#include "utils.h"
void floatToBytes(float val, uint8_t *bytes)
{
union
{
float f;
uint8_t bytes[4];
} u;
u.f = val;
// 考虑大小端问题
for (int i = 0; i < 4; i++)
{
bytes[i] = u.bytes[i];
}
}
2024-12-27 10:34:06 +08:00
float bytesToFloat(uint8_t *bytes)
{
union
{
float f;
uint8_t bytes[4];
} u;
for (int i = 0; i < 4; i++)
{
u.bytes[i] = bytes[i];
}
return u.f;
2024-12-27 09:47:56 +08:00
}