字符串操作-C語言

 

/*****************************************************************************
FileName : string.c
Function : 字符串各種操作函數
Author   : mike
Email    : [email protected]	
Version  : V1.0
Date     : 2019-07-12
Note     : 
*****************************************************************************/
#define u8                  unsigned char  
u8 char_to_hex(u8 c) //字符轉HEX
{
    if(c >= 'A' && c <= 'F') {
        c = c - 'A' + 10;
    } else if(c >= 'a' && c <= 'f') {
        c = c - 'a' + 10;
    } else if(c >= '0' && c <= '9') {
        c -= '0';
    } else {
        c = 0xff;
    }
    return c;
}

//0~15轉成字符,必須跟spi_bt_rfinfo_read在同個bank
char hex_to_char(u8 num)//字符轉HEX
{
    if (num >= 10) {
        return ('A' + num - 10);
    } else {
        return ('0' + num);
    }
}

int my_strlen(const char *cpSrc)
{
	const char *cpTemp = cpSrc;
	while(*cpTemp++);
	return cpTemp-cpSrc-1;
}
 
/*******************************************************************************
* Function Name  : my_memset
* Description    : write value to mem
* Input          : *dst: destination pointer
*                  val : value
*                  cnt :length
* Output         : None
* Return         : None
*******************************************************************************/
/**/
void my_memset(void *dst, BYTE val, int cnt)
{

	BYTE *pDstTemp = (BYTE *)dst;
	while (cnt--)
	{
		*pDstTemp++ = val;
	}
}

/*******************************************************************************
* Function Name  : zeromem
* Description    : clear mem to 0
* Input          : *dst: destination pointer
*                  cnt :length
* Output         : None
* Return         : None
*******************************************************************************/
void zeromem(void *dst, int cnt)
{
	my_memset(dst, 0, cnt);
}

/*******************************************************************************
* Function Name  : my_memcpy
* Description    : copy data from src to dst
* Input          : *dst: destination pointer
*                  *src: source pointer
*                  cnt :length
* Output         : None
* Return         : None
*******************************************************************************/
void my_memcpy(void *dst, void *src, int cnt)
{

	BYTE *pDstTemp = (BYTE *)dst;
	BYTE *pSrcTemp = (BYTE *)src;
	while (cnt--)
	{
		*pDstTemp++ = *pSrcTemp++;
	}
}
void u16memset(void *dst, BYTE val, int cnt)
{
	u16 *pdst = (u16*)dst;
	int cnt_temp = cnt/2;
	u16 u16val = (val<<8)|(val);
	while(cnt_temp--)
	{
		*pdst++ = u16val;
	}

	if(cnt > ((cnt>>1)<<1))
	{
		u16val = *pdst;
		u16val &= 0xff00;
		u16val |= val;
		*pdst = u16val;
	}
}
void u16memcpy(void *dst, void *src, int cnt)
{
	u16 *pDstTemp = (u16 *)dst;
	u16 *pSrcTemp = (u16 *)src;
	int cnt_temp = cnt/2;
	
	while(cnt_temp--)
	{
		*pDstTemp++ = *pSrcTemp++;
	}

	if(cnt > ((cnt>>1)<<1))
	{
		*pDstTemp &= 0xff00;
		*pDstTemp |= (*pSrcTemp & 0x00ff);
	}
}

/*******************************************************************************
* Function Name  : dma_memcpy
* Description    : copy data from src to dst
* Input          : *dst: destination pointer
*                  *src: source pointer
*                  cnt :length
* Output         : None
* Return         : None
*******************************************************************************/
/* Compare memory to memory */
int my_mem_cmp(const void* dst, const void* src, u8 cnt)
{
    const char *d = (const char *)dst, *s = (const char *)src;
    int r = 0;
    while(cnt-- && (r = *d++ - *s++) == 0) ;
    return r;
}

/*******************************************************************************
* Function Name  : bmemcmp
* Description    : compare src and dst data same or different
* Input          : *dst: destination pointer
*                  *src: source pointer
*                  cnt :length
* Output         : None
* Return         : 0:different; 1:same
*******************************************************************************/
BOOL bmemcmp(void *dst, void *src, u32 byte_len)
{
    int i = 0;
    u8 *dst_u8 = (u8*)dst;
    u8 *src_u8 = (u8*)src;
    for(i=0;i < byte_len;)
    {
       if(dst_u8[i] != src_u8[i]) 
       {
          return 0;
       }   
       i++;     
    }
    return 1;
}

/*******************************************************************************
* Function Name  : datacmp
* Description    : u32 data compare
* Input          : dst: destination data
*                  src: source data
* Output         : None
* Return         : 0:same; 1:dst>src ; -1:dst<src
*******************************************************************************/
s8 datacmp(u32 dst, u32 src)
{

	s8 res = 0;
	if(dst > src)
		res = 1;
	else if(dst == src)
		res = 0;
	else if(dst < src)
		res = -1;

	return res;
}


/**
 * strcpy - Copy a %NUL terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 */
char * _strcpy(char * dest, const char *src) 
{
	char *tmp = dest;

	while ((*dest++ = *src++) != '\0');
	return tmp;
}

/**
 * strncmp - Compare two length-limited strings
 * @cs: One string
 * @ct: Another string
 * @count: The maximum number of bytes to compare
 */
int _strncmp(const char * cs, const char * ct, unsigned int count) 
{
	register signed char __res = 0;

	while (count) {
		if ((__res = *cs - *ct++) != 0 || !*cs++)
			break;
		count--;
	}

	return __res;
}

/*******************************************************************************
* Function Name  : XCHDWORD
* Description    : exchange u32 data high_low byte
* Input          : Before the exchange of data
* Output         : None
* Return         : After an exchange of data
*******************************************************************************/
u32 XCHDWORD(u32 dwXCHData)
{
	u32 dwXCHDataTmp;

	((BYTE *)(&dwXCHDataTmp))[0] = ((BYTE *)(&dwXCHData))[3];
	((BYTE *)(&dwXCHDataTmp))[1] = ((BYTE *)(&dwXCHData))[2];
	((BYTE *)(&dwXCHDataTmp))[2] = ((BYTE *)(&dwXCHData))[1];
	((BYTE *)(&dwXCHDataTmp))[3] = ((BYTE *)(&dwXCHData))[0];

	return dwXCHDataTmp;
}

/*******************************************************************************
* Function Name  : XCHWORD
* Description    : exchange u32 data high_low byte
* Input          : Before the exchange of data
* Output         : None
* Return         : After an exchange of data
*******************************************************************************/
u32 XCHWORD(u32 wXCHData)
{
	u32 wXCHDataTmp;

	((BYTE *)(&wXCHDataTmp))[0] = ((BYTE *)(&wXCHData))[1];
	((BYTE *)(&wXCHDataTmp))[1] = ((BYTE *)(&wXCHData))[0];

	return wXCHDataTmp;
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章