我有一个类似于此00133587a1bddb8dae00a3a01a010100
的十六进制字符串,它实际上是7个十六进制字符串连接在扩展时看起来像00 133587a1 bddb8dae 00a3a01a 01 01 00
。我正在尝试将这些值中的前5个扫描到此结构
typedef struct __param_value{
uint8_t sytem_id;
uint8_t comp_id;
uint16_t seq;
uint8_t frame;
uint16_t command;
uint8_t current;
uint8_t autocontinue;
float param1;
float param2;
float param3;
float param4;
float x;//param7
float y;//param8
float z;//param9
uint8_t fwt;
}param_value
和最后2个进入这些变量
int txtseq;
int cont=1;
使用sscanf,像这样
sscanf(in_str,"%2x%8x%8x%8x%2x%2x%2x",&(points[wp].seq),&(points[wp].x),&(points[wp].y),&(points[wp].z),&(points[wp].fwt),&txtseq,&cont);
但我无法弄清楚正确的语法。有可能这样做吗?
答案 0 :(得分:2)
您必须传递unsigned int
的地址才能扫描%x
格式说明符的数据。然后,您可以转换为正确的数据类型。
#include <stdio.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef struct param_value{
uint8_t sytem_id;
uint8_t comp_id;
uint16_t seq;
uint8_t frame;
uint16_t command;
uint8_t current;
uint8_t autocontinue;
float param1;
float param2;
float param3;
float param4;
float x;//param7
float y;//param8
float z;//param9
uint8_t fwt;
}param_value;
int main(void) {
char hexstr [] = "00133587a1bddb8dae00a3a01a010100";
unsigned v1, v2, v3, v4, v5, v6, v7;
param_value rec;
int txtseq;
int cont;
if (7 != sscanf(hexstr, "%2x%8x%8x%8x%2x%2x%2x", &v1, &v2, &v3, &v4, &v5, &v6, &v7))
{
printf ("Bad scan\n");
return 1;
}
rec.seq = (uint16_t)v1;
rec.x = (float)v2;
rec.y = (float)v3;
rec.z = (float)v4;
rec.fwt = (uint8_t)v5;
txtseq = (int)v6;
cont = (int)v7;
printf("%u %f %f %f %u %d %d\n", rec.seq, rec.x, rec.y, rec.z,
rec.fwt, txtseq, cont);
return 0;
}
节目输出:
0 322275232.000000 3185282560.000000 10723354.000000 1 1 0
但是:你还没有提到y
值bddb8dae
是否是负面的。