1
我有以下幾點:如何正確地結合4個字節,使浮子(單)
payload.data[i].data = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
注:payload.data[i].data
如果float類型。
使用此:printf("test:%X%X%X%X", buf[8], buf[9], buf[10], buf[11]);
,我已確認緩衝區包含#42C78A3D
或DEC 99.77(粗略)。
此:printf("Float value:%f", payload.data[i].data
打印出1120373248.00,這是DEC的#42C78A00
在我看來,由於某種原因,buf[11]
快到了空。
這裏是我的代碼更完整視圖:
int i = 0;
int j = 0;
struct sensor_payload payload;
payload.key = (buf[0] << 8) | buf[1];
payload.id = (buf[2] << 8) | buf[3];
payload.type = (buf[4] << 8) | buf[5];
payload.fields = buf[6];
for(i = 0, j = 0; i < payload.fields; i++, j = j +33){
payload.data[i].data_type = buf[j+7];
payload.data[i].data = (buf[j+8] << 24) | (buf[j+9] << 16) | (buf[j+10] << 8) | (buf[j+11]);
slog(0, SLOG_DEBUG, "test:%X%X%X%X", buf[8], buf[9], buf[10], buf[11]);
}
payload.valid = true;
return payload;
和定義:
struct sensor_data{
uint8_t data_type;
float data;
};
struct sensor_payload{
uint16_t key, id, type;
uint8_t fields;
struct sensor_data data[4];
bool valid;
};
是的,這是故意的。對於測試,我只是試圖在buf []中打印一個可能的4個浮點數。 –