IMO: your method will work.
Simplify the problem.
Promotion uint16_tup uint32_tand promotion int16_tis int32_teasy to understand and achieve the expected results. Zero fills in unsigned integers and the sign extends the signed integers.
int main() {
uint16_t u = 65535;
int16_t i = -1;
printf("%" PRIu32 "\n", (uint32_t) u);
printf("%" PRId32 "\n", (int32_t) i);
return 0;
}
65535
-1
int16_t uint32_t int16_t int32_t, uint32_t. ( , , int16_t int int32_t, uint32_t, sizeof int <= sizeof int32_t.) , , .
uint16_t int32_t uint16_t int32_t. .
. "", "".
int main() {
uint16_t u = 65535;
int16_t i = -1;
printf("%" PRIu32 "\n", (uint32_t) i);
printf("%" PRId32 "\n", (int32_t) u);
return 0;
}
4294967295
65535