/*
* file name : my_itoa.c
* function : achieve itoa and atoi
* date : 2012-5-3
* modify : 2012-5-7
* author : gaoxiang
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int my_atoi(const char *str);
int main (void)
{
int i = -10;
char str[50];
my_itoa(i,str,2);
printf("%s\n",str);
// printf("%d",my_atoi("-87"));
return EXIT_SUCCESS;
}
/*function: my_itoa()*/
int my_itoa(int num, char *str, int radix) /*可以用sprintf(str,"%x",100)*/
{
int sign = 0;
char *temp = str;
int i = 0;
int j = 0;
if (num == 0) /*处理0*/
{
*temp = '0';
*(temp+1) = '\0';
return 1;
}
(num < 0) ? (sign = -1) : (sign = 1); /*处理负数*/
(sign == -1) ? (num = -num) : (num = num);
while(num > 0){ /*求余,算出每一位的值*/
*temp++ = "0123456789abcdef"[num%rad
C语言实现的 itoa(int num, str *temp, int radix) 与 atoi(str *temp)
最新推荐文章于 2021-05-22 02:41:01 发布