user-0 提问: 下列十六进制数据 变长的，该如何解析拆开这些数据

回复:
user: user-1
content: #include <stdio.h>  int main(int argc, char *argv[]) {     FILE *fp;     char  ch;     int   stage;     int   len;     int   i;     char buf[] = {         0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,         0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,         0x03, 0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00     };      fp = fmemopen(buf, sizeof(buf), "r");     stage = 0;     while ((ch = fgetc(fp)) >= 0) {         switch (stage) {         case 0:             printf("cmd: %02x\n", (unsigned char)ch);             stage++;             break;         case 1:             printf("len: %02x\n", (unsigned char)ch);             len = ch;             stage++;             break;         case 2:             printf("val: %02x",   (unsigned char)ch);             for (i = 1; i < len * 3; i++) {                 ch = fgetc(fp);                 if (i % 3 == 0) {                     printf(",");                 }                 printf(" %02x", (unsigned char)ch);             }             printf("\n");             stage = 0;             break;         default:             break;         }     }     return 0; }  /* output: cmd: 01 len: 04 val: 00 00 00, 00 00 03, 00 00 00, 00 00 05 cmd: 02 len: 03 val: 00 00 00, 00 00 00, 00 00 08 cmd: 03 len: 02 val: 00 00 08, 00 00 00 */

user: user-0
content: 谢谢赵四老师

user: user-2
content: 有限状态自动机 #include <stdio.h> unsigned char b[55]={ 0x01,0x04,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x05,0x02,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x03,0x02,0x00,0x00,0x08,0x00,0x00,0x00, 0x01,0x04,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x05,0x03,0x02,0x00,0x00,0x08,0x00,0x00,0x00}; unsigned char c; int i; int s; int bi; int pi; int pn; int p; int main() {     s=0;     i=0;     while (1) {         if (i>=55) break;         c=b[i];         if (s==0) {             if (c==0x01) {                 printf("cmd 01\n");                 s=1;             } else if (c==0x02) {                 printf("cmd 02\n");                 s=1;             } else if (c==0x03) {                 printf("cmd 03\n");                 s=1;             } else {                 printf("Invalid cmd %02X at %d\n",c,i);                 break;             }         } else if (s==1) {             pn=(int)c;             printf("pn:%d\n",pn);             bi=0;             pi=0;             s=2;         } else {//s==2             if (bi==0) {                 p=0x10000*(int)c;                 bi++;             } else if (bi==1) {                 p=p+0x100*(int)c;                 bi++;             } else if (bi==2) {                 p=p+(int)c;                 pi++;                 printf("p%d:%d\n",pi,p);                 if (pi>=pn) {                     s=0;                 } else {                     bi=0;                 }             }         }         i++;     }     return 0; } //cmd 01 //pn:4 //p1:0 //p2:3 //p3:0 //p4:5 //cmd 02 //pn:3 //p1:0 //p2:0 //p3:8 //cmd 03 //pn:2 //p1:8 //p2:0 //cmd 01 //pn:4 //p1:0 //p2:3 //p3:0 //p4:5 //cmd 03 //pn:2 //p1:8 //p2:0 //

user: user-3
content: 尾码大致如此 struct s{  ch tag;  int len;  char* data; } while(！文件结尾) //begin s s1; read(s1.tag); if(tag==1||tag==2||tag==3) {   read(s1.len);   read(s1.data,s1.len); } //end;

user: user-4
content: 还是这个尴尬局面： 你要是能把问题描述清楚，那么你自己也就解决了。 既然你描述不清，那别人也不知道问题是什么，更谈不到解决。

user: user-0
content: 谢谢了，结构体肯定用的   01 04 00 00 00 00 00 03 00 00 00 00 00 05 02 03 00 00 00 00 00 00 00 00 08 03 02 00 00 08 00 00 00     01 02 03 可能都存在，也可能存在一个或者2个，这个判断如何判断，还有存在的话， 命令后面数值个数也是不固定的，也会变化，这个怎么判断好。谢谢

user: user-0
content: 现在我的主要疑问是

user: user-5
content: 如果是确定的长度和确定的位置，可以考虑创建结构体，然后把这些数值（应该是放在一个缓存里的吧，并且缓存是char or unsigned char)强制转换成结构体变量，然后根据变化的值来定后面要读取多少个字节～