#include "Common.h" #include /********************************************************************************** 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= '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>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