Replies: 1 comment 1 reply
-
memcpy() is better portability-wise |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
In the Zephyr RTOS environment, I'm looking for a clean way to convert a float to a uint32_t (bitwise, preserving the IEEE 754 representation) and back again.
I'm aware of the sys_put_be32, sys_get_le16, etc., functions for byte order handling, but I haven't found anything like a sys_float_to_u32() or similar.
Right now, I'm using the following approach:
`#include <stdint.h>
union FloatUInt32 {
float f;
uint32_t u;
};
// Convert float to uint32_t (bitwise)
uint32_t float_to_uint32(float value) {
union FloatUInt32 data;
data.f = value;
return data.u;
}
// Convert uint32_t to float (bitwise)
float uint32_to_float(uint32_t value) {
union FloatUInt32 data;
data.u = value;
return data.f;
}
`
This works fine, but I’m wondering:
Is there a built-in or recommended way to do this in Zephyr?
Is there any concern with strict aliasing or portability using the union approach?
Would it be better to use memcpy() in this context?
Appreciate any advice or pointers. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions