123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #include "Common.h"
- #include <string.h>
- /**********************************************************************************
- ModemStrCmp
- ***********************************************************************************/
- int ModemStrCmp(char *msg,char *str)
- {
- char *p1=str;
- char *p2=msg;
- while(*p1!=0){
- if(*p1!=*p2)return 1;
- p1++;
- p2++;
- }
- return 0;
- }
- unsigned char AsciiHexStringToHexBytes(char *src, unsigned char *des)
- {//"3031"-->0x30,0x31
- unsigned char temp[2];
- unsigned char i,k;
- int len;
-
- len=strlen(src);
- if(len % 2) return 0;//不能为单数
- for(i=0;i<len/2;i++){
- temp[0]=src[2*i];
- temp[1]=src[2*i+1];
- for(k=0;k<2;k++){
- if(temp[k] >= '0' && temp[k] <= '9') temp[k] -= 0x30;
- else if(temp[k] >= 'a' && temp[k] <= 'f') temp[k] -= 0x57;
- else if(temp[k] >= 'A' && temp[k] <= 'F') temp[k] -= 0x37;
- }
- des[i] = ((temp[0]<<4)&0xf0) | temp[1];
- }
- return i;
- }
- /*******************************************************************
- *GetParaFromStr
- *从Str中找到Para=后面至';'或非字符的字串并放入Value
- 返回Value的长度
- 举例:Str="GT+SMP=IP=192.168.1.1;Port=12345"
- 如果Para="Port" 则Value将被赋值为"12345",并返回5
- 如果Para="IP" 则Value将被赋值为"192.168.1.1",并返回11
- 要求Para长度不大于20字节 ,Value长度不大于40字节
- ********************************************************************/
- int GetParaFromStr(char *Str,char *Para,char *Value)
- {
- int ValueLen=0,ParaLen=0;
- char ParaTemp[22];
- char *p;
- char d;
- int i=0;
- if(0==*Str || 0==*Para)return 0;
- while(0!=(d=*Para) && ParaLen<20){
- ParaTemp[i++]=d;
- if(d=='=' || d==';')return 0;
- Para++;
- ParaLen++;
- }
- ParaTemp[ParaLen++]='=';
- ParaTemp[ParaLen]=0;
- p=strstr(Str,ParaTemp);
- p+=ParaLen;
- //--
- while(*p>0x20 && ';'!=*p && ValueLen<=80){
- *Value=*p;
- p++;
- Value++;
- ValueLen++;
- }
- *Value=0;
- return ValueLen;
- }
- unsigned char AscToHex(unsigned char aHex)
- {
- if((aHex>=0)&&(aHex<=9))
- aHex += 0x30;
- else if((aHex>=10)&&(aHex<=15))//A-F
- //aHex += 0x37;
- aHex += 0x57;
- else aHex = 0xff;
- return aHex;
- }
- void AscStrToHexStr(char *AscStr, char *HexStr)
- {
- char *pAscStr=AscStr;
- unsigned char d,h,l;
- while(0!=(d=(unsigned char)*pAscStr++)){
- l=d&0x0f;
- h=d>>4;
- *HexStr++=AscToHex(h);
- *HexStr++=AscToHex(l);
- }
- *HexStr=0;
- }
- //"3132" --> 0x31,0x32
- void StrAsciiToHex(char *src, unsigned char *des)
- {
- unsigned char temp[2],i;
- if(strlen(src)%2) return;
- while(0!=*src)
- {
- for(i=0;i<2;i++)
- {
- temp[i] = *src++;
- if(temp[i] >= '0' && temp[i] <= '9') temp[i] -= 0x30;
- else if(temp[i] >= 'A' && temp[i] <= 'F') temp[i] -= 0x37;
- else temp[i] -= 0x57;
- }
- temp[0] <<= 4;
- temp[0] &= 0xf0;
- *des++=temp[0]|temp[1];
- }
- *des=0;
- }
- void MakeStrEndByNewLine(char *str)
- {//一定要以'\r'结尾
- unsigned short i;
-
- for(i=0;i<strlen(str);i++)
- {
- if(str[i] == '\r')
- {
- str[i]=0;
- return;
- }
- }
- }
- void ReplaceStrWithN(char *str)
- {
- char d;
- char *p=str;
- while(0!=(d=*p)){
- if(d=='\r' || d=='\n'){
- *p='\0';
- break;
- }
- p++;
- }
- }
- //从source查找长度为targetLen目标的target,找到后返回目标段后一字节的索引,没找到返回-1,source结束为sourceEndIndicator
- //witchOne: 找第几个目标
- short FindTargetIndex(char *source, char sourceEndIndicator, char *target, unsigned char targetLen,unsigned char witchOne)
- {
- unsigned short targetIndex;
- unsigned char targetNum;
- targetIndex=0;
- targetNum=0;
- while(*source != sourceEndIndicator)
- {
- if(0==memcmp(source, target, targetLen))
- {//找到了
- targetNum++;
- if(targetNum == witchOne)
- return (targetIndex+targetLen);
- }
- source++;
- targetIndex++;
- }
- return -1;
- }
- void AscStrTurnHexStr(char *AscStr, char *HexStr)
- {
- char *pAscStr=AscStr;
- int i=4;
- unsigned char d,h,l;
- while(i){
- d=(unsigned char)*pAscStr++;
- l=d&0x0f;
- h=d>>4;
- *HexStr++=AscToHex(h);
- *HexStr++=AscToHex(l);
- i--;
- }
- *HexStr=0;
- }
- //从字符串中获取第n个逗号的内容索引
- //没找到或者内容为空返回小于0成功返回内容首索引值
- short getSegMent(char *msg,unsigned char whichOne){
- short ret;
- unsigned short i;
- unsigned char thisOne=0;
-
- for(i=0;i<strlen(msg);i++){
- if(msg[i]==','){
- if(++thisOne==whichOne){
- if(msg[i+1]==',') return -1;
- else return (i+1);
- }
- }
- }
- return -2;
- }
|