求教拆分字符串的问题,多谢!
请帮忙看一下代码问题,想拆分出如下结果:
0000002C
00000003
00000004
3133353030303030303030000000000000000000000000000
01
33320000
32353000
02
char line[]="0000002C000000030000000431333530303030303030300000000000000000000001333200003235300002";
char Mes_Length[8];
char Com_ID[8];
char Seq_Num[8];
char Blk_num[42];
char Blk_type[2];
char Pro_code[8];
char City_code[8];
char Re_times[2];
strncpy(Mes_Length,line+0,8);
printf("%s \n",Mes_Length);
strncpy(Com_ID,line+8,8);
printf("%s \n",Com_ID);
strncpy(Seq_Num,line+16,8);
printf("%s \n",Seq_Num);
strncpy(Blk_num,line+24,42);
printf("%s \n",Blk_num);
strncpy(Blk_type,line+66,2);
printf("%s \n",Blk_type);
strncpy(Pro_code,line+68,8);
printf("%s \n",Pro_code);
strncpy(City_code,line+76,8);
printf("%s \n",City_code);
strncpy(Re_times,line+84,2);
printf("%s \n",Re_times);
但是通过上面代码拆分出来的结果如下:
0000002C
000000030000002C
00000004000000030000002C
31333530303030303030300000000000000000000000000004000000030000002C
01
3332000001
323530003332000001
02323530003332000001
,,,求教问题在哪,多谢
[解决办法]
没有尾0
[解决办法]
- C/C++ code
char line[]="0000002C000000030000000431333530303030303030300000000000000000000001333200003235300002";char Mes_Length[ 9]; //0000002Cchar Com_ID [ 9]; //00000003char Seq_Num [ 9]; //00000004char Blk_num [43]; //313335303030303030303000000000000000000000char Blk_type [ 3]; //01char Pro_code [ 9]; //33320000char City_code [ 9]; //32353000char Re_times [ 3]; //02strncpy(Mes_Length,line+ 0, 8);Mes_Length[ 8]=0;printf("Mes_Length=[%s]\n",Mes_Length);strncpy(Com_ID ,line+ 8, 8);Com_ID [ 8]=0;printf("Com_ID =[%s]\n",Com_ID );strncpy(Seq_Num ,line+16, 8);Seq_Num [ 8]=0;printf("Seq_Num =[%s]\n",Seq_Num );strncpy(Blk_num ,line+24,42);Blk_num [42]=0;printf("Blk_num =[%s]\n",Blk_num );strncpy(Blk_type ,line+66, 2);Blk_type [ 2]=0;printf("Blk_type =[%s]\n",Blk_type );strncpy(Pro_code ,line+68, 8);Pro_code [ 8]=0;printf("Pro_code =[%s]\n",Pro_code );strncpy(City_code ,line+76, 8);City_code [ 8]=0;printf("City_code =[%s]\n",City_code );strncpy(Re_times ,line+84, 2);Re_times [ 2]=0;printf("Re_times =[%s]\n",Re_times );
[解决办法]
每次截完将最后一位 = '\0'就可以了
[解决办法]
- C/C++ code
char Mes_Length[ 8+1]; //0000002Cchar Com_ID [ 8+1]; //00000003char Seq_Num [ 8+1]; //00000004char Blk_num [42+1]; //313335303030303030303000000000000000000000char Blk_type [ 2+1]; //01char Pro_code [ 8+1]; //33320000char City_code [ 8+1]; //32353000char Re_times [ 2+1]; //02