Please consider the following code:
float float_value = x; // x is any valid float value
int int_value = 0;
size_t size = sizeof(int) < sizeof(float) ? sizeof(int) : sizeof(float);
memcpy(&int_value, &float_value, size);
As far as i know this could result in an trap representation. My questions:
The sanctioned way which won't produce any trap representation is
unsigned char obj[sizeof float];
memcpy(obj, &float_value, sizeof float);
Then you can use the bytes of the object representation to build your desired int
.
But using fixed-width integers as mentioned by Stephen Canon is better - unless you have a weird float
size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With