C語言根據(jù)協(xié)議分割獲取字符串單元
微信公眾號(hào):楊源鑫
關(guān)注可了解更多的教程及排版技巧。問題或建議,請(qǐng)公眾號(hào)留言;
如果你覺得本公眾號(hào)對(duì)你有幫助,歡迎贊賞!!
第一次用MarkDown寫公眾號(hào)文章,希望多多支持,技術(shù)也需要升級(jí)!
協(xié)議做如下規(guī)定
規(guī)定數(shù)據(jù)協(xié)議:
序列號(hào)????長(zhǎng)度????狀態(tài)字????數(shù)據(jù)長(zhǎng)度???數(shù)據(jù)1???數(shù)據(jù)2????數(shù)據(jù)3
以空格作為數(shù)據(jù)單元。
例如:
00001?00007?1???3??34567?26358?32698???
?(1)???(2)?(3)?(4)??(5)???(6)???(7)
以上各個(gè)編號(hào)字段代表的基本含義如下:
(1)00001就是數(shù)字1,即代表序列號(hào)為1
(2)00007就是數(shù)字7,即代表長(zhǎng)度為7
(3)1代表狀態(tài)字
(4)3代表數(shù)字長(zhǎng)度
(5)34567代表數(shù)據(jù)1
(6)26358代表數(shù)據(jù)2
(7)32698代表數(shù)據(jù)3
這樣就找到規(guī)律了,假設(shè)數(shù)據(jù)都為整型或者負(fù)整型,我們就可以根據(jù)這個(gè)規(guī)則來實(shí)現(xiàn)以下代碼:
#include?
#include?
#include?
//根據(jù)空格拆分字符串?
int?partition(char?*src,?char?*par,?int?pos)
{
??int?i,j;
??i?=?pos;
??//取得一個(gè)非空字符?
??while(src[i]?==?'?')
????++i;
??if(src[i]?!=?'\0')
??{
????j?=?0;
????while((src[i]?!=?'\0')?&&?(src[i]?!=?'?'))
????{
????????//判斷條件是否滿足?
????????if((src[i]?>?'9')?||?(src[i]?'0')?&&?(src[i]?!=?'-'))
????????????return?-1?;
??????par[j]?=?src[i];
??????++i;
??????++j;
????}
????par[j]='\0';
????return?i;
??}
??else
????return?-1;
}
int?main(void)
{
????int?serial_number?;
????int?lenght?;
????int?status?;
????int?data_length;
????int?data1,data2,data3;?
????int?position?=?0;
????int?para_flag?=?1?;?
????int?parameter_item?=?0;
????char?partition_string[20]?=?{0};
????char?*data?=?"00001?00007?1?3?34567?26358?32698";?
????while(para_flag)
????{
????????if(para_flag?==?0)
????????????break?;?
????????if((position?=?partition(data,partition_string,position))?!=?-1)
????????{
????????????++parameter_item?;
????????????switch(parameter_item)
????????????{
????????????????case?1:
????????????????????serial_number?=?atoi(partition_string);
????????????????????break?;
????????????????case?2:
????????????????????lenght?=?atoi(partition_string);
????????????????????break?;
????????????????case?3:
????????????????????status?=?atoi(partition_string);????
????????????????????break?;
????????????????case?4:
????????????????????data_length?=?atoi(partition_string);???
????????????????????break?;
????????????????case?5:
????????????????????data1?=?atoi(partition_string);?
????????????????????break?;
????????????????case?6:
????????????????????data2?=?atoi(partition_string);?
????????????????????break?;
????????????????case?7:?
????????????????????data3?=?atoi(partition_string);?
????????????????????para_flag?=?0?;
????????????????????break?;
????????????}
????????}
????}
????printf("序號(hào):%d\n",serial_number);
????printf("長(zhǎng)度:%d\n",lenght);
????printf("狀態(tài)字:%d\n",status);
????printf("數(shù)據(jù)長(zhǎng)度:%d\n",data_length);
????printf("數(shù)據(jù)1:%d\n",data1);
????printf("數(shù)據(jù)2:%d\n",data2);
????printf("數(shù)據(jù)3:%d\n",data3);
????return?0;???
}?
運(yùn)行結(jié)果:
另外,推薦一下韋東山老師的嵌入式課程:
移動(dòng)互聯(lián)網(wǎng),人工智能,物聯(lián)網(wǎng)等技術(shù)正在飛速發(fā)展,這都離不開嵌入式技術(shù)的支持,如果您想創(chuàng)業(yè)自己開發(fā)產(chǎn)品,韋東山老師的視頻一定可以幫助您完成創(chuàng)客夢(mèng)想。
[分享]韋東山嵌入式linux 第1期 ARM裸機(jī)實(shí)戰(zhàn)
https://j.youzan.com/zRcvW9
[分享]韋東山嵌入式linux第2期 ?驅(qū)動(dòng)大全
https://j.youzan.com/o3nvW9
[分享]韋東山嵌入式linux 第3期 項(xiàng)目實(shí)戰(zhàn)
https://j.youzan.com/z0cvW9
[分享]韋東山第4期 Android系統(tǒng)視頻
https://j.youzan.com/ifMlW9
[分享]韋東山嵌入式Linux arm 不帶s3c2440開發(fā)板java入門視頻
https://j.youzan.com/1VnvW9
[分享]韋東山嵌入式linux視頻不帶ARM9開發(fā)板c++入門 手把手指導(dǎo)
https://j.youzan.com/NdnvW9
[分享]韋東山嵌入式Linux SPI arm視頻不帶s3c2440開發(fā)板 手把手指導(dǎo)
https://j.youzan.com/xHcvW9
[分享]錄制完畢韋東山嵌入式Linux視頻JZ2440s3c2440開發(fā)板設(shè)備樹詳解
https://j.youzan.com/lmnvW9
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問題,請(qǐng)聯(lián)系我們,謝謝!