common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "common.h"
  2. unsigned long htoi(char s[])
  3. {
  4. int i = 0;
  5. unsigned long long n = 0;
  6. if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) i = 2;
  7. else i = 0;
  8. for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i){
  9. if (tolower(s[i]) > '9') n = 16 * n + (10 + tolower(s[i]) - 'a');
  10. else n = 16 * n + (tolower(s[i]) - '0');
  11. }
  12. return n;
  13. }
  14. int GetParaFromStr(char *Str,char *Para,char *Value){
  15. int ValueLen=0,ParaLen=0;
  16. char ParaTemp[22];
  17. char *p;
  18. char d;
  19. int i=0;
  20. if(0==*Str || 0==*Para)return 0;
  21. while(0!=(d=*Para) && ParaLen<20){
  22. ParaTemp[i++]=d;
  23. if(d=='=' || d==';')return 0;
  24. Para++;
  25. ParaLen++;
  26. }
  27. ParaTemp[ParaLen++]='=';
  28. ParaTemp[ParaLen]=0;
  29. p=strstr(Str,ParaTemp);
  30. p+=ParaLen;
  31. //--
  32. while(*p>0x20 && ';'!=*p && ValueLen<=80){
  33. *Value=*p;
  34. p++;
  35. Value++;
  36. ValueLen++;
  37. }
  38. *Value=0;
  39. return ValueLen;
  40. }
  41. char msgCmp(char *msg, char *target){
  42. char *p1=target, *p2=msg;
  43. while(*p1 != 0){
  44. if(*p1 != *p2) return 0;
  45. p1++;p2++;
  46. }
  47. return 1;
  48. }
  49. // '56'->0X56
  50. unsigned char charStrToHex(char d1, char d2){
  51. unsigned char p1=d1;
  52. unsigned char p2=d2;
  53. if(p1>='0' && p1 <='9') p1 -= 0x30;
  54. else if(p1>='a' && p1 <='f') p1 -= 0x57;
  55. else if(p1>='A' && p1 <='F') p1 -= 0x37;
  56. else return 0;
  57. p1<<=4;p1&=0xF0;
  58. if(p2>='0' && p2 <='9') p2 -= 0x30;
  59. else if(p2>='a' && p2 <='f') p2 -= 0x57;
  60. else if(p2>='A' && p2 <='F') p2 -= 0x37;
  61. else return 0;
  62. return (p1|p2);
  63. }
  64. /*
  65. restoreDataFormatByHex
  66. "313233"=>{0x31,0x32,0x33}
  67. */
  68. unsigned char restoreDataFormatByHex(unsigned char *src, unsigned short len)
  69. {
  70. unsigned short i,j,k;
  71. unsigned char temp[2];
  72. if(0==src) return 1;
  73. j=0;
  74. for(i=0;i<len/2;i++){
  75. temp[0]=src[2*i];
  76. temp[1]=src[2*i+1];
  77. for(k=0;k<2;k++){
  78. if(temp[k]>='0' && temp[k]<='9') temp[k] -= 0x30;
  79. else if(temp[k]>='a' && temp[k]<='f') temp[k] -= 0x57;
  80. else if(temp[k]>='A' && temp[k]<='F') temp[k] -= 0x37;
  81. else return 2;
  82. }
  83. temp[0] <<= 4;
  84. temp[0] &= 0xF0;
  85. src[j++] = temp[0] | temp[1];
  86. }
  87. return 0;
  88. }
  89. /*
  90. 从长度为len的src缓存中查找第index个target,并返回下一个字节的索引
  91. src:缓存
  92. len:缓存长度
  93. target:要匹配的字节内容
  94. index:第几个target, 1,2,3,4.....
  95. return: < 0 失败,else, target后面一个字节的索引
  96. */
  97. int findByteFromStr(unsigned char *src, int len,unsigned char target,unsigned char index){
  98. int i;
  99. unsigned char num=0;
  100. for(i=0;i<len;i++){
  101. if(src[i]==target){
  102. if(++num==index) return i+1;
  103. }
  104. }
  105. return -1;
  106. }