📄 misc.c
字号:
#include "common.h"
int strncmp(char *p, char *q, int n)
{
while(n--) {
if (*p++!=*q++) return 1;
}
return 0;
}
int strnlen(char *p,uint max)
{
uint l=0;
while (*p++) l++;
if (l<max) return l;
else return max;
}
void strcpy(char *p, char *q)
{
do {
*p++ = *q;
} while(*q++);
}
void memset(char *p, int len, unsigned char n)
{
while(len--) {
*p++ = n;
}
return;
}
void strlow(char *strin)
{
char *str = strin;
while (*str) {
if (*str<='Z' && *str>='A') {
*str = *str - 'A' + 'a';
}
str++;
}
}
uint xtoi (char *str,int *n)
{
uint u;
char c;
char *old = str;
while (*str && *str == ' ') {
str += 1;
}
// skip preceeding zeros
while (*str && *str == '0') {
str += 1;
}
// skip preceeding white space
if (*str && (*str == 'x' || *str == 'X')) {
str += 1;
}
// convert hex digits
u = 0;
c = *(str++);
while (c) {
if (c >= 'a' && c <= 'f') {
c -= 'a' - 'A';
}
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
u = u << 4 | c - (c >= 'A' ? 'A'-10 : '0');
} else {
break;
}
c = *(str++);
}
if (n) *n = str - old;
return u;
}
/********************************************************************************
Function: tolower
Parameters: uint8 ch - ASCII character to be converted lowercase
Return val: uint8 - converted character
Date: 21.8.2002
Desc: If ch is UPPERCASE letter it is converted to lowercase and
returned. Otherwise original character is returned
*********************************************************************************/
uint8 tolower (uint8 ch)
{
if( (ch < 91) && (ch > 64) )
return(ch + 32);
return(ch);
}
/********************************************************************************
Function: toupper
Parameters: uint8 ch - ASCII character to be converted UPPERCASE
Return val: uint8 - converted character
Date: 21.8.2002
Desc: If ch is lowercase letter it is converted to UPPERCASE and
returned. Otherwise original character is returned
*********************************************************************************/
uint8 toupper (uint8 ch)
{
if( (ch < 123) && (ch > 96) )
return(ch - 32);
return(ch);
}
/* Is the given ASCII code numerical */
/* e.g. '0','1','2' ... '9' */
uint8 isnumeric (uint8 ch)
{
if( (ch < 58) && (ch > 47) )
return(1);
return(0);
}
/* HexToAscii - Take one byte and return its two ASCII */
/* values for both nibbles */
uint hextoascii (uint8 c)
{
uint8 ch = c;
uint8 as1;
uint8 as2;
/* take the char and turn it to ASCII */
as1 = ch;
as1 >>= 4;
as1 &= 0x0F;
if ( as1<10 )
as1 += 48;
else
as1 += 55;
as2 = ch;
as2 &= 0x0F;
if ( as2<10 )
as2 += 48;
else
as2 += 55;
return( ((uint)(as1)<<8) + as2 );
}
/* Convert ASCII character to numerical */
/* e.g. '1' -> 0x01, 'A' ->0x0A */
uint8 asciitohex (uint8 ch)
{
if( (ch < 58) && (ch > 47) )
return(ch - 48);
if( (ch < 71 ) && (ch > 64) )
return(ch - 55);
return 0;
}
void ltoa (uint nmbr, uint8 *ch )
{
/* Transforms value of long word to ASCII string */
/* Makes it iterative */
uint multiple;
uint decade,comp;
uint8 i,found;
/* Init String */
for( i=0; i<10;i++ )
*ch++ = '0';
ch -= 10;
/* See if Zero */
if(nmbr == 0) {
*ch++ = '0';
*ch = '\0';
}
decade = 1000000000;
found = FALSE;
for( i=0; i<10; i++) {
if(i != 0)
decade /= 10;
for( multiple=9; multiple>0; multiple--) {
if( (i==0) && (multiple > 2) )
continue;
comp = decade * multiple;
if(nmbr >= comp) {
*ch = hextoascii(multiple);
nmbr -= comp;
found = TRUE;
break; /* Still processing */
}
}
if( found == TRUE)
ch++;
}
*ch = '\0'; /* EOL */
}
void itoa (uint nmbr, uint8* ch )
{
/* Transforms value of word to ASCII string */
/* Makes it iterative */
uint decade, multiple;
uint comp;
uint8 i,found;
/* Init String */
for( i=0; i<5;i++)
*ch++ = '0';
ch -= 5;
/* See if Zero */
if(nmbr == 0) {
*ch++ = '0';
*ch = '\0';
}
decade = 10000;
found = FALSE;
for( i=0; i<5; i++) {
if(i != 0)
decade /= 10;
for( multiple=9; multiple>0; multiple--) {
if( (i==0) && (multiple > 6) )
continue;
comp = decade * multiple;
if(nmbr >= comp) {
*ch = hextoascii(multiple);
nmbr -= comp;
found = TRUE;
break; /* Still processing */
}
}
if( found == TRUE)
ch++;
}
*ch = '\0'; /* EOL */
}
/* Convert given buffer containing ASCII numbers */
/* to numerical positive int value (max. 32767) */
int atoi (uint8 *buf, uint8 buflen)
{
int oval = 0;
uint8 nval = 0;
while(buflen--) {
if(*buf == '\0')
break;
if( isnumeric(*buf) == 0 )
return(-1);
nval = asciitohex(*buf++);
oval = oval * 10;
oval += nval;
/* Overflow? */
if(oval < nval)
return(-1);
}
return(oval);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -