首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这样的数据结构该怎样提取?该如何解决

2012-03-09 
这样的数据结构该怎样提取?有这样一组数据:445400079CA8110224032900它的数据内容为:445400:DT,07:07年,9C

这样的数据结构该怎样提取?
有这样一组数据:
44   54   00   07   9C   A8   11   02   24   03   29   00
它的数据内容为:
44   54   00:DT,     07:07年,   9C   A8:特征字;
11   02   24   03   29   00:110天,22时,40分,32.900秒
其中时间段的天、时分秒分别占用了下一个字节的8个位,我想用structure
定义一个结构变量:
type   structure{
char     id[3];       //DT
int       yy;             //07
char     name[2]     //9CA8
int       jd;             //110
int       hh;             //22
int       mm;               //40
float   ss;               //32.900
}
实际上这样定义只能提取出前2个字符(DT),提取其它的数据则完全错误,
该如何定义这个结构变量?请各位高人帮忙!先谢谢了!

[解决办法]
方法很多,下面的方法只是一个例子:
$ cat 0722_001.c
/*
* http://community.csdn.net/Expert/topic/5668/5668420.xml?temp=.2454492
*/

#include <stdio.h>
#include <string.h>

#define PRINT(x) printf( "%s = %s\n ", #x, x)

typedef struct {
unsigned char id[2 + 1]; /* DT */
unsigned char yy[2 + 1]; /* 07 */
unsigned char name[4 + 1]; /* 9CA8 */
unsigned char jd[3 + 1]; /* 110 */
unsigned char hh[2 + 1]; /* 40 */
unsigned char mm[2 + 1]; /* 40 */
unsigned char ss[6 + 1]; /* 32.900 */
} MYMTOM;

static unsigned char buf[] =
{
0x44, 0x54, 0x00, 0x07,
0x9C, 0xA8, 0x11, 0x02,
0x24, 0x03, 0x29, 0x00
};

int main(void)
{
MYMTOM x;

memset(&x, 0, sizeof(x));
memcpy(x.id, buf, sizeof(x.id));
sprintf(x.yy, "%02hhX ", buf[3]);
sprintf(x.name, "%02hhX%02hhX ", buf[4], buf[5]);
sprintf(x.jd, "%02hhX%.1hhX ", buf[6], buf[7] > > 4);
sprintf(x.hh, "%.1hhX%.1hhX ", buf[7] & 0x0F, buf[8] > > 4);
sprintf(x.mm, "%.1hhX%.1hhX ", buf[8] & 0x0F, buf[9] > > 4);
sprintf(x.ss, "%.1hhX%.1hhX.%.1hhX%02hhX ",
buf[9] & 0x0F, buf[10] > > 4,
buf[10] & 0x0F, buf[11]);
PRINT(x.id);
PRINT(x.yy);
PRINT(x.name);
PRINT(x.jd);
PRINT(x.hh);
PRINT(x.mm);
PRINT(x.ss);

return 0;
}

$ make CFLAGS=-Wall run
cc -Wall 0722_001.c -o 0722_001
./0722_001
x.id = DT
x.yy = 07
x.name = 9CA8
x.jd = 110
x.hh = 22
x.mm = 40
x.ss = 32.900
$

热点排行