大家好!
我正在创建一个将十六进制转换为十进制的函数。 该函数假设接受参数、它转换参数(十六进制)并返回参数(十进制)。 当我执行该函数时、我收到一条错误消息、指出类型不匹配。 我想转换类型以缓解该问题。 该参数来自压力传感器。 压力传感器以 uint32_t 形式返回、我还获取该数据的移动平均值、这些数据以浮点数形式返回。 我需要将这两个值转换为十进制。 我尝试在函数中更改数据类型以匹配压力数据。 这不奏效。 我希望保持压力数据不变。
以下是位于源文件中的函数代码:
float Hex2Dec(float hex[17]) { long decimal; // return value long place; int i = 0; // iterator int val; int len; decimal = 0; place = 1; // Find total length of Hex number // Go to the beginning of the number len = strlen(hex); len--; // Iterate over the number for (i = 0; hex[i] != '\0'; i++) { // Find decimal representation of hex[i] if (hex[i] >= '0' && hex[i] <= '9') { val = hex[i] - 48; } else if (hex[i] >= 'a' && hex[i] <= 'f') { val = hex[i] - 97 + 10; } else if (hex[i] >= 'A' && hex[i] <= 'F') { val = hex[i] - 65 + 10; } decimal += val * pow(16, len); len--; } return decimal; }` Here is the header file: ` float Hex2Dec(float hex[17]); ` Here is the code used in my main file: ` // These are global variables static uint8_t Press0[4] = {0xFFU, 0xFFU, 0xFFU, 0xFFU}; static uint32_t bridge_data0; static uint32_t pressure0; static uint32_t temp_data0; static uint32_t temperature0; // this is a local variable inside a switch case function // the rest of the case function is irrelevant mvg_avg_p0 = MovingAvg(pressure0); Hex2Dec(pressure0); Hex2Dec(mvg_avg_p0); *((uint32_t*)Press0) = i2cReadHWPressureTemp(4); // channel 2 (2^2 = 4) bridge_data0 = ((uint32_t)(Press0[0] & ~STATUS_MASK) << 8) | Press0[1]; pressure0 = ((bridge_data0 - OUTPUT_MIN)*(PRESSURE_MAX_001BA - PRESSURE_MIN_001BA))/ (OUTPUT_MAX - OUTPUT_MIN) + PRESSURE_MIN_001BA; temp_data0 = ((uint32_t)(Press0[2]) << 8) | ((uint32_t)(Press0[3] >> 5)); temperature0 = (((temp_data0/2047)*200) - 50); `
我迷路了。 有人能帮忙吗?