c语言字符串整数转换

//  http:blog.csdn.net/vinfcent/article/details/63310056
//****字符串转十六
int StrToHex(char *str, int num)
{
if (*str == '0')
{
str+=2;

}
while(*str != '\0')
{
if ((*str >= '0')&&(*str <= '9'))
{
num = num*16 + (*str - '0');
str++;
}
else if (((*str >= 'a')&&(*str <= 'f')))
{
num =num*16+(*str - 'a')+10;
str++;
}
else if (((*str >= 'A')&&(*str <= 'F')))
{
num =num*16+(*str - 'A')+10;
str++;
}
}
if (*str == '-')
{
num = -num;
}
return num;
}


//*****字符串转整数
int StrToInt(char *str, int num)
{
if (*str == '-')
{
str++;
}
while(*str != '\0')
{
    num = num*10 + (*str - '0');
str++;
}
if (*str == '-')
{
num = -num;
}
return num;
}




//*****整数转字符串
void IntToStr(int num,char str[])
{
int i = 0,j=0;
char buff[32] = {0};
int n = num;
if (num < 0)
{
num = -num;
}
do 
{
buff[i++] = num%10 +'0';
num = num/10;


} while (num);
if (n < 0)
{
buff[i++] = '-';
}
buff[i] = '\0';

i--;
int cnt=i;
for (;j <= cnt; j++)
{
str[j] = buff[i--];

}
str[j] = '\0';


}




//*****除法及取模实现函数,不考虑负数
int funDiv(int a,int b, bool flag)
{
int i = 0;
if(b == 0)//不能为0
{
return;
}

while(a >= b){
a = a-b;
i++;
}
if (flag)//  /
{
return i;
} 
else// %
{
return a;
}
}
//******实现库函数memcpy()
void mymemcpy(void *dest,const void *src,int cnt)
{
char *dest_t = (char*)dest;
const char *src_t = (const char*)src;
while(cnt--)
{
*dest_t = *src_t;
dest_t++;
src_t++;
}
}

猜你喜欢

转载自blog.csdn.net/Vinfcent/article/details/63310056