decode添加大小端序放入判断
This commit is contained in:
parent
6c5fc45750
commit
bd48e17fa6
72
decode.c
72
decode.c
@ -31,6 +31,21 @@ typedef struct {
|
||||
} info_t;
|
||||
#pragma pack()
|
||||
|
||||
// 检测系统字节序
|
||||
static uint8_t is_little_endian() {
|
||||
uint16_t test = 0x0001;
|
||||
return *(uint8_t *)&test;
|
||||
}
|
||||
|
||||
// 字节序转换函数
|
||||
static uint16_t swap_uint16(uint16_t val) {
|
||||
return (val << 8) | (val >> 8);
|
||||
}
|
||||
|
||||
static int16_t swap_int16(int16_t val) {
|
||||
return (val << 8) | ((val >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
void print_usage() {
|
||||
printf("用法:\n");
|
||||
printf("decode.exe -f <文件名> [-o <输出文件名>]\n");
|
||||
@ -45,12 +60,20 @@ void print_usage() {
|
||||
|
||||
// 原有的decode函数保持不变
|
||||
void decode(char *filename) {
|
||||
|
||||
|
||||
FILE *file = fopen(filename, "rb");
|
||||
if (!file) {
|
||||
fprintf(stderr, "无法打开文件\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检测系统字节序
|
||||
uint8_t sys_is_le = is_little_endian();
|
||||
// 显示系统字节序
|
||||
fprintf(stderr, "系统字节序: %s\n",
|
||||
sys_is_le ? "小端序" : "大端序");
|
||||
|
||||
// 获取文件大小
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
@ -68,24 +91,39 @@ void decode(char *filename) {
|
||||
|
||||
while ((records_read = fread(buffer, sizeof(info_t), BUFFER_SIZE, file)) > 0) {
|
||||
for (size_t i = 0; i < records_read; i++) {
|
||||
// 直接使用小端序数据,无需转换
|
||||
// 单字节数据:y, month, d, h, m, s
|
||||
// 双字节数据:ms, deepth, ax, ay, az, gx, gy, gz 已经是正确的小端序
|
||||
// 如果系统是大端序,需要转换所有16位数据
|
||||
uint16_t ms = buffer[i].ms;
|
||||
int16_t depth = buffer[i].deepth;
|
||||
int16_t ax = buffer[i].data.ax;
|
||||
int16_t ay = buffer[i].data.ay;
|
||||
int16_t az = buffer[i].data.az;
|
||||
int16_t gx = buffer[i].data.gx;
|
||||
int16_t gy = buffer[i].data.gy;
|
||||
int16_t gz = buffer[i].data.gz;
|
||||
|
||||
if (!sys_is_le) {
|
||||
// 系统是大端序,需要转换
|
||||
ms = swap_uint16(ms);
|
||||
depth = swap_int16(depth);
|
||||
ax = swap_int16(ax);
|
||||
ay = swap_int16(ay);
|
||||
az = swap_int16(az);
|
||||
gx = swap_int16(gx);
|
||||
gy = swap_int16(gy);
|
||||
gz = swap_int16(gz);
|
||||
}
|
||||
|
||||
printf("%04d,%02d,%02d,%02d,%02d,%02d,%03d,%04d,%d,%d,%d,%d,%d,%d\n",
|
||||
buffer[i].y + 2000, // 年份偏移2000
|
||||
buffer[i].month, // 月
|
||||
buffer[i].d, // 日
|
||||
buffer[i].h, // 时
|
||||
buffer[i].m, // 分
|
||||
buffer[i].s, // 秒
|
||||
buffer[i].ms, // 毫秒,uint16_t
|
||||
buffer[i].deepth, // 深度,uint16_t
|
||||
buffer[i].data.ax, // 加速度计x,int16_t
|
||||
buffer[i].data.ay, // 加速度计y,int16_t
|
||||
buffer[i].data.az, // 加速度计z,int16_t
|
||||
buffer[i].data.gx, // 陀螺仪x,int16_t
|
||||
buffer[i].data.gy, // 陀螺仪y,int16_t
|
||||
buffer[i].data.gz // 陀螺仪z,int16_t
|
||||
buffer[i].y + 2000, // 单字节数据不需要转换
|
||||
buffer[i].month,
|
||||
buffer[i].d,
|
||||
buffer[i].h,
|
||||
buffer[i].m,
|
||||
buffer[i].s,
|
||||
ms, // 使用可能转换过的值
|
||||
depth,
|
||||
ax, ay, az,
|
||||
gx, gy, gz
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user