#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
uint32_t lee_strlen(const uint8_t *str)
{
const uint8_t *p = str;
if (p == NULL) return 0;
while (*p != '\0') p++;
return p - str;
}
uint32_t lee_find(const uint8_t *s1, uint32_t sz1, const uint8_t *s2, uint32_t sz2, uint8_t const **pRes, uint32_t resLen)
{
if (NULL == s1 || NULL == s2 || sz1 < sz2 || sz2 == 0)
{
return 0;
}
const uint8_t *p1 = s1;
const uint8_t *p2 = s2;
uint32_t i, j;
uint8_t found = 0;
uint32_t ret = 0;
for (i = 0; i <= sz1 - sz2;)
{
found = 1;
p1 = s1 + i;
for (j = 0; j < sz2; j++)
{
if (p1[j] != p2[j])
{
found = 0;
break;
}
}
if (found)
{
if (pRes != NULL && ret < resLen) pRes[ret] = p1;
i += sz2;
ret++;
}
else
{
i++;
}
//if ((NULL!=pRes) && (ret >= resLen - 1)) break;
}
return ret;
}
uint32_t lee_strstr(uint8_t *s1, const uint8_t *s2, uint8_t const **pRes, uint32_t resLen)
{
return lee_find(s1, lee_strlen(s1), s2, lee_strlen(s2), pRes, resLen);
}
uint32_t lee_split(uint8_t *s1, const uint8_t *s2, uint8_t **pRes, uint32_t resLen)
{
uint32_t cnt = lee_strstr(s1, s2, (uint8_t const **)pRes,resLen);
uint32_t ret = cnt;
uint32_t l2 = lee_strlen(s2);
if (pRes)
{
if (l2 > 0)
{
while (cnt > 0)
{
pRes[cnt] = pRes[cnt - 1];
*(pRes[cnt]) = '\0';
pRes[cnt] += l2;
cnt--;
}
}
pRes[0] = s1;
}
return ret + 1;
}
uint32_t lee_hex2str(uint8_t *hex, uint16_t hexLen, uint8_t *str)
{
uint8_t i = 0;
uint8_t hexMap[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
for (i = 0; i < hexLen; i++)
{
*str = hexMap[hex[i] >> 4];
*(str + 1) = hexMap[hex[i] & 0x0f];
str += 2;
}
*str = 0;
return i * 2;
}
uint8_t lee_hex2asc(const uint8_t* hexStr)
{
uint8_t chr=0;
uint8_t tmp=0;
uint8_t i;
if (hexStr == NULL) return 0;
for (i=0;i<2;i++,hexStr++)
{
tmp=0;
if (*hexStr>='0' && *hexStr<='9')
{
tmp=*hexStr-'0';
}
else if (*hexStr>='A' && *hexStr<='F')
{
tmp=*hexStr-'A'+10;
}
else if (*hexStr>='a' && *hexStr<='f')
{
tmp=*hexStr-'a'+10;
}
chr=(chr<<4)+tmp;
}
return chr;
}
uint8_t lee_crc8(uint8_t init_val, uint8_t *data, uint8_t size)
{
//uint8_t crc = 0;
uint8_t i;
for (i = 0; i < size; i++)
{
init_val ^= data[i];
}
return init_val;
}
int32_t lee_atoi(const uint8_t *str)
{
uint8_t flag = 0;
int32_t res = 0;
if (str == NULL) return res;
while (*str != '\0')
{
if (*(str) >= '0' && *(str) <= '9') break;
if (*str == '-' && *(str + 1) >= '0' && *(str + 1) <= '9')
{
str++;
flag = 1;
break;
}
str++;
}
while (*str != '\0')
{
if (*str >= '0' && *str <= '9') res = res * 10 + (*str - '0');
else break;
str++;
}
return (flag ? (0 - res) : res);
}
uint8_t lee_itoa(int32_t value, uint8_t base, uint8_t *result)
{
// check that the base if valid
uint32_t len = 1;
uint8_t *ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
if (base < 2 || base > 36 || result == NULL)
{
return 0;
}
do
{
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
}
while (value);
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
len = ptr - ptr1 + 1;
while (ptr1 < ptr)
{
tmp_char = *ptr;
*ptr-- = *ptr1;
*ptr1++ = tmp_char;
}
//result[len]='\0';
return len;
}
uint8_t* lee_strcpy(uint8_t *d, const uint8_t *s)
{
return (uint8_t *)strcpy((char *)d, (const char *)s);
}
uint8_t lee_strcmp(const uint8_t *d, const uint8_t *s)
{
if (d != NULL && s != NULL)
{
do
{
if (*d == '\0' && *s == '\0') return 0;
if (*d != *s)
{
return *d - *s;
}
d++;
s++;
}
while (1);
//return 0;
}
return (d == s) ? 0 : -1;
}
uint8_t lee_stricmp(const uint8_t *d, const uint8_t *s)
{
if (d != NULL && s != NULL)
{
do
{
if (*d == '\0' && *s == '\0') return 0;
if (((*d >= 'A' && *d <= 'Z') ? (*d + 'a' - 'A') : *d) !=
((*s >= 'A' && *s <= 'Z') ? (*s + 'a' - 'A') : *s))
{
return *d - *s;
}
d++;
s++;
}
while (1);
//return 0;
}
return (d == s) ? 0 : -1;
}
uint8_t* lee_strncpy(uint8_t *d, uint8_t *s, uint32_t sz)
{
return (uint8_t *)strncpy((char *)d, (const char *)s, sz);
}
uint32_t lee_strncmp(uint8_t *s1, uint8_t *s2, uint32_t sz)
{
return strncmp((const char *)s1, (const char *)s2, sz);
}
void lee_strUp(uint8_t *str, uint32_t sz)
{
if (NULL == str) return;
uint8_t delta = 'a' - 'A';
while (sz)
{
if (*str >= 'a' && *str <= 'z')
{
(*str) -= delta;
}
sz--;
str++;
}
return;
}
void lee_strLow(uint8_t *str, uint32_t sz)
{
if (NULL == str) return;
uint8_t delta = 'a' - 'A';
while (sz)
{
if (*str >= 'A' && *str <= 'Z')
{
(*str) += delta;
}
sz--;
str++;
}
return;
}
void swap(char *a, char *b )
{
char t=*a;
*a=*b;
*b=t;
}
char *reverse(char *s,uint16_t len)
{
char *b=s, *e=s+len-1;
while( b<e )
{
swap(b++,e--);
}
return s;
}
char *dec2bin(uint16_t n)
{
static char str[33];
uint16_t i=0;
do {
str[i++]=n%2+'0';
n/=2;
}while( n );
str[i]=0;
return reverse(str,i);
}
uint16_t bin2dec(char *s)
{
int i,n=0;
for(i=0;s[i];i++)
n=n*2+s[i]-'0';
return n;
}
uint32_t str_to_hex(const char *bufin, uint32_t len, char *bufout)
{
uint32_t i = 0;
if (NULL == bufin || len <= 0 || NULL == bufout)
{
return -1;
}
for(i = 0; i < len; i++)
{
sprintf(bufout+i*2, "%02X", bufin[i]);
}
return 0;
}
void HexStrToStr(const uint8_t *source, uint8_t *dest, uint32_t sourceLen)
{
short i;
uint8_t highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
return ;
}
//10进制转16进制
void Int_To_Hex(unsigned int value, char buffer[], int length)
{
unsigned int i=(sizeof(unsigned int)*2);
unsigned int temp;
int j=0;
while(i--)
{
temp = (value&(0xf<<(4*i)))>>(4*i);
if(temp>9)
buffer[j] = 'A'+temp-10;
else
buffer[j] = '0'+temp;
j++;
}
buffer[length] = '\0';
}
/* 返回ch字符在sign数组中的序号 */
static int getIndexOfSigns(char ch)
{
if(ch >= '0' && ch <= '9')
{
return ch - '0';
}
if(ch >= 'A' && ch <='F')
{
return ch - 'A' + 10;
}
if(ch >= 'a' && ch <= 'f')
{
return ch - 'a' + 10;
}
return -1;
}
/* 十六进制数转换为十进制数 */
long hexToDec(char *source)
{
long sum = 0;
long t = 1;
int i, len;
len = strlen(source);
for(i=len-1; i>=0; i--)
{
sum += t * getIndexOfSigns(*(source + i));
t *= 16;
}
return sum;
}