sys.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * author wulianwei
  3. * title 一般工具
  4. */
  5. #include "sys.h"
  6. //C库
  7. #include <stdarg.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. //字节流转HEX字符串
  11. void byteToHexStr(const unsigned char* source, char* dest, int sourceLen)
  12. {
  13. short i;
  14. unsigned char highByte, lowByte;
  15. for (i = 0; i < sourceLen; i++)
  16. {
  17. highByte = source[i] >> 4;
  18. lowByte = source[i] & 0x0f ;
  19. highByte += 0x30;
  20. if (highByte > 0x39)
  21. dest[i * 2] = highByte + 0x07;
  22. else
  23. dest[i * 2] = highByte;
  24. lowByte += 0x30;
  25. if (lowByte > 0x39)
  26. dest[i * 2 + 1] = lowByte + 0x07;
  27. else
  28. dest[i * 2 + 1] = lowByte;
  29. }
  30. return ;
  31. }
  32. /**
  33. * 字母大小写转换, flag = 1 转大写, flag =0 转小写
  34. */
  35. void letterSwitch(char *str, int flag)
  36. {
  37. while (*str != '\0')
  38. {
  39. if (flag)
  40. {
  41. if (*str >= 'a' && *str <= 'z')
  42. {
  43. *str = *str - 32;
  44. }
  45. }
  46. else
  47. {
  48. if (*str >= 'A' && *str <= 'Z')
  49. {
  50. *str = *str + 32;
  51. }
  52. }
  53. str++;
  54. }
  55. }
  56. void inttohex(uint16 value,uint8 *hex)
  57. {
  58. uint16 x1;
  59. hex[0]=value/(16*16*16)+'0';
  60. x1=value%(16*16*16);
  61. hex[1]=x1/(16*16)+'0';
  62. x1=value%(16*16);
  63. hex[2]=x1/16+'0';
  64. x1=value%16;
  65. hex[3]=x1+'0';
  66. hex[4]=' ';
  67. }
  68. /**
  69. * 整形字符串转整形数字
  70. */
  71. int my_atoi(char *str)
  72. {
  73. int n = 0;
  74. int flag = 0;
  75. if(*str == '-')
  76. {
  77. flag = 1;
  78. str++;
  79. }
  80. while(*str >= '0' && *str <= '9')
  81. {
  82. n = n*10 + (*str - '0');
  83. str++;
  84. }
  85. if(flag == 1)
  86. {
  87. n = -n;
  88. }
  89. return n;
  90. }
  91. /**
  92. * 整形转字符串
  93. * num:待转的数据,str:目标地址,radix:进制2,8,10,16
  94. */
  95. char* my_itoa(int num,char* str,int radix)
  96. {
  97. char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表
  98. unsigned unum;//存放要转换的整数的绝对值,转换的整数可能是负数
  99. int i=0,j,k;//i用来指示设置字符串相应位,转换之后i其实就是字符串的长度;转换后顺序是逆序的,有正负的情况,k用来指示调整顺序的开始位置;j用来指示调整顺序时的交换。
  100. //获取要转换的整数的绝对值
  101. if(radix==10&&num<0)//要转换成十进制数并且是负数
  102. {
  103. unum=(unsigned)-num;//将num的绝对值赋给unum
  104. str[i++]='-';//在字符串最前面设置为'-'号,并且索引加1
  105. }
  106. else unum=(unsigned)num;//若是num为正,直接赋值给unum
  107. //转换部分,注意转换后是逆序的
  108. do
  109. {
  110. str[i++]=index[unum%(unsigned)radix];//取unum的最后一位,并设置为str对应位,指示索引加1
  111. unum/=radix;//unum去掉最后一位
  112. }while(unum);//直至unum为0退出循环
  113. str[i]='\0';//在字符串最后添加'\0'字符,c语言字符串以'\0'结束。
  114. //将顺序调整过来
  115. if(str[0]=='-') k=1;//如果是负数,符号不用调整,从符号后面开始调整
  116. else k=0;//不是负数,全部都要调整
  117. char temp;//临时变量,交换两个值时用到
  118. for(j=k;j<=(i-1)/2;j++)//头尾一一对称交换,i其实就是字符串的长度,索引最大值比长度少1
  119. {
  120. temp=str[j];//头部赋值给临时变量
  121. str[j]=str[i-1+k-j];//尾部赋值给头部
  122. str[i-1+k-j]=temp;//将临时变量的值(其实就是之前的头部值)赋给尾部
  123. }
  124. return str;//返回转换后的字符串
  125. }
  126. /**
  127. * @Title split,分割字符串
  128. * @Param src:源数据, separator:分割符, dest:目标地址, size:分割数量
  129. */
  130. void split(char *src,const char *separator,char *dest[],int size)
  131. {
  132. char *pNext;
  133. int count = 0;
  134. if (src == NULL || strlen(src) == 0)
  135. return;
  136. if (separator == NULL || strlen(separator) == 0)
  137. return;
  138. pNext = strtok(src,separator);
  139. while(pNext != NULL)
  140. {
  141. dest[count] = pNext;
  142. ++count;
  143. if(size <= count)
  144. break;
  145. pNext = strtok(NULL,separator);
  146. }
  147. }
  148. /**
  149. * @Title split,分割字符串
  150. * @Param src:源数据, separator:分割符, dest:目标地址, limit:分割限制数量
  151. */
  152. void splitCharLimit(char *str, char seprator, char *dest[], int limit)
  153. {
  154. int i, length, ct = 0, start = -1;
  155. length = strlen(str);
  156. for (i = 0; i <= length; i++) {
  157. if (start == -1 && limit <= ct + 1) {
  158. dest[ct] = &str[i];
  159. break;
  160. }
  161. if (str[i] == seprator) {
  162. if (start != -1) {
  163. dest[ct] = &str[start]; /* set up pointer */
  164. ct++;
  165. }
  166. str[i] = '\0'; /* add a null char; make a C string */
  167. start = -1;
  168. } else {
  169. if (start == -1) {
  170. start = i;
  171. }
  172. }
  173. }
  174. }
  175. uint8_t memmem(uint8_t * result,uint16_t result_length, uint8_t *rdystring,uint8_t rdylen)
  176. {
  177. for(int i=0;i<result_length-rdylen;i++){
  178. if(strstr((char * )result+i,(char *)rdystring) != NULL)
  179. {
  180. return 1;
  181. }
  182. }
  183. return 0;
  184. }