Common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "Common.h"
  2. #include <string.h>
  3. /**********************************************************************************
  4. ModemStrCmp
  5. ***********************************************************************************/
  6. int ModemStrCmp(char *msg,char *str)
  7. {
  8. char *p1=str;
  9. char *p2=msg;
  10. while(*p1!=0){
  11. if(*p1!=*p2)return 1;
  12. p1++;
  13. p2++;
  14. }
  15. return 0;
  16. }
  17. unsigned char AsciiHexStringToHexBytes(char *src, unsigned char *des)
  18. {//"3031"-->0x30,0x31
  19. unsigned char temp[2];
  20. unsigned char i,k;
  21. int len;
  22. len=strlen(src);
  23. if(len % 2) return 0;//不能为单数
  24. for(i=0;i<len/2;i++){
  25. temp[0]=src[2*i];
  26. temp[1]=src[2*i+1];
  27. for(k=0;k<2;k++){
  28. if(temp[k] >= '0' && temp[k] <= '9') temp[k] -= 0x30;
  29. else if(temp[k] >= 'a' && temp[k] <= 'f') temp[k] -= 0x57;
  30. else if(temp[k] >= 'A' && temp[k] <= 'F') temp[k] -= 0x37;
  31. }
  32. des[i] = ((temp[0]<<4)&0xf0) | temp[1];
  33. }
  34. return i;
  35. }
  36. /*******************************************************************
  37. *GetParaFromStr
  38. *从Str中找到Para=后面至';'或非字符的字串并放入Value
  39. 返回Value的长度
  40. 举例:Str="GT+SMP=IP=192.168.1.1;Port=12345"
  41. 如果Para="Port" 则Value将被赋值为"12345",并返回5
  42. 如果Para="IP" 则Value将被赋值为"192.168.1.1",并返回11
  43. 要求Para长度不大于20字节 ,Value长度不大于40字节
  44. ********************************************************************/
  45. int GetParaFromStr(char *Str,char *Para,char *Value)
  46. {
  47. int ValueLen=0,ParaLen=0;
  48. char ParaTemp[22];
  49. char *p;
  50. char d;
  51. int i=0;
  52. if(0==*Str || 0==*Para)return 0;
  53. while(0!=(d=*Para) && ParaLen<20){
  54. ParaTemp[i++]=d;
  55. if(d=='=' || d==';')return 0;
  56. Para++;
  57. ParaLen++;
  58. }
  59. ParaTemp[ParaLen++]='=';
  60. ParaTemp[ParaLen]=0;
  61. p=strstr(Str,ParaTemp);
  62. p+=ParaLen;
  63. //--
  64. while(*p>0x20 && ';'!=*p && ValueLen<=80){
  65. *Value=*p;
  66. p++;
  67. Value++;
  68. ValueLen++;
  69. }
  70. *Value=0;
  71. return ValueLen;
  72. }
  73. unsigned char AscToHex(unsigned char aHex)
  74. {
  75. if((aHex>=0)&&(aHex<=9))
  76. aHex += 0x30;
  77. else if((aHex>=10)&&(aHex<=15))//A-F
  78. //aHex += 0x37;
  79. aHex += 0x57;
  80. else aHex = 0xff;
  81. return aHex;
  82. }
  83. void AscStrToHexStr(char *AscStr, char *HexStr)
  84. {
  85. char *pAscStr=AscStr;
  86. unsigned char d,h,l;
  87. while(0!=(d=(unsigned char)*pAscStr++)){
  88. l=d&0x0f;
  89. h=d>>4;
  90. *HexStr++=AscToHex(h);
  91. *HexStr++=AscToHex(l);
  92. }
  93. *HexStr=0;
  94. }
  95. //"3132" --> 0x31,0x32
  96. void StrAsciiToHex(char *src, unsigned char *des)
  97. {
  98. unsigned char temp[2],i;
  99. if(strlen(src)%2) return;
  100. while(0!=*src)
  101. {
  102. for(i=0;i<2;i++)
  103. {
  104. temp[i] = *src++;
  105. if(temp[i] >= '0' && temp[i] <= '9') temp[i] -= 0x30;
  106. else if(temp[i] >= 'A' && temp[i] <= 'F') temp[i] -= 0x37;
  107. else temp[i] -= 0x57;
  108. }
  109. temp[0] <<= 4;
  110. temp[0] &= 0xf0;
  111. *des++=temp[0]|temp[1];
  112. }
  113. *des=0;
  114. }
  115. void MakeStrEndByNewLine(char *str)
  116. {//一定要以'\r'结尾
  117. unsigned short i;
  118. for(i=0;i<strlen(str);i++)
  119. {
  120. if(str[i] == '\r')
  121. {
  122. str[i]=0;
  123. return;
  124. }
  125. }
  126. }
  127. void ReplaceStrWithN(char *str)
  128. {
  129. char d;
  130. char *p=str;
  131. while(0!=(d=*p)){
  132. if(d=='\r' || d=='\n'){
  133. *p='\0';
  134. break;
  135. }
  136. p++;
  137. }
  138. }
  139. //从source查找长度为targetLen目标的target,找到后返回目标段后一字节的索引,没找到返回-1,source结束为sourceEndIndicator
  140. //witchOne: 找第几个目标
  141. short FindTargetIndex(char *source, char sourceEndIndicator, char *target, unsigned char targetLen,unsigned char witchOne)
  142. {
  143. unsigned short targetIndex;
  144. unsigned char targetNum;
  145. targetIndex=0;
  146. targetNum=0;
  147. while(*source != sourceEndIndicator)
  148. {
  149. if(0==memcmp(source, target, targetLen))
  150. {//找到了
  151. targetNum++;
  152. if(targetNum == witchOne)
  153. return (targetIndex+targetLen);
  154. }
  155. source++;
  156. targetIndex++;
  157. }
  158. return -1;
  159. }
  160. void AscStrTurnHexStr(char *AscStr, char *HexStr)
  161. {
  162. char *pAscStr=AscStr;
  163. int i=4;
  164. unsigned char d,h,l;
  165. while(i){
  166. d=(unsigned char)*pAscStr++;
  167. l=d&0x0f;
  168. h=d>>4;
  169. *HexStr++=AscToHex(h);
  170. *HexStr++=AscToHex(l);
  171. i--;
  172. }
  173. *HexStr=0;
  174. }
  175. //从字符串中获取第n个逗号的内容索引
  176. //没找到或者内容为空返回小于0成功返回内容首索引值
  177. short getSegMent(char *msg,unsigned char whichOne){
  178. short ret;
  179. unsigned short i;
  180. unsigned char thisOne=0;
  181. for(i=0;i<strlen(msg);i++){
  182. if(msg[i]==','){
  183. if(++thisOne==whichOne){
  184. if(msg[i+1]==',') return -1;
  185. else return (i+1);
  186. }
  187. }
  188. }
  189. return -2;
  190. }