简体   繁体   中英

converting between number representations cpp and python

I am converting a float in cpp (on an arduino) to an integer representation using the following code:

float temp = 22.8;
uint32_t temp_as_integer;
static_assert(sizeof(temp) == sizeof(temp_as_integer), "sizes don't match");
memcpy(&temp_as_integer, &temp, sizeof(temp_as_integer));

this gets sent to another computer via serial and then needs to be converted back into a float in python.

This is where I am stuck. Could someone kindly point me in the right direction to do the inverse of the cpp process in python?

Integers and floats have a totally different representation internally. If you are sending it to another computer the chances of it being converted back to the right float are very slim (especially if the chipset architecture is different or if one is big endian and the other is little endian). Perhaps it's worth serializing it into a string? Can you afford to do that in terms of performance/bandwidth?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM