编辑代码

#include <stdio.h>
#include <stdint.h>

// 将两个16位无符号整数合并为一个float
float combine_16bit_to_float(uint16_t high, uint16_t low) {
    // 使用联合体将两个uint16_t合并为uint32_t
    union {
        uint32_t u;
        float f;
    } float_union;

    // 合并高16位和低16位
    float_union.u = ((uint32_t)high << 16) | (uint32_t)low;

    return float_union.f;
}

int main() {
    uint16_t high = 0x42F6;
    uint16_t low = 0xE666;
    uint16_t temp_buf[8] ;

    float result;
    float result1;
    temp_buf[2]=  0x42F6;
    temp_buf[3]= 0xE666;

    result = combine_16bit_to_float(high, low);
    result1 = combine_16bit_to_float(temp_buf[2], temp_buf[3]);

    printf("High part: 0x%04X\n", high);
    printf("Low part: 0x%04X\n", low);
    printf("High part: 0x%04X\n", temp_buf[2]);
    printf("Low part: 0x%04X\n",  temp_buf[3]);
    printf("Resulting float: %f\n", result);
    printf("Resulting1 float: %f\n", result1);

    return 0;
}