#include <msvcrt/errno.h>
#include <msvcrt/stdlib.h>
#include <msvcrt/internal/file.h>
char* _itoa(int value, char* string, int radix)
{
char tmp[33];
char* tp = tmp;
int i;
unsigned v;
int sign;
char* sp;
if (radix > 36 || radix <= 1)
{
__set_errno(EDOM);
return 0;
}
sign = (radix == 10 && value < 0);//1.如果是10进制,则要判断符号
if (sign)
v = -value;//2.如果为负,则取绝对值
else
v = (unsigned)value;
while (v || tp == tmp)//3.两个条件一个也不能少,因为v为0时也到得到0结果,而不能结束函数
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+''0'';//tp和结果的顺序正好相反
else
*tp++ = i + ''a'' - 10;
}
if (string == 0)//4.string为null则为string申请空间,大小为数字长度+符号+'\0';
string = (char*)malloc((tp-tmp)+sign+1);
sp = string;//5.将string赋给sp,以便返回
if (sign)//6.如果有符号,则加入
*sp++ = ''-'';
while (tp > tmp)//7.将tp反转,则正好是结果
*sp++ = *--tp;
*sp = 0;//8.string的末尾置为'\0'
return string;
}
#include <msvcrt/stdlib.h>
#include <msvcrt/internal/file.h>
char* _itoa(int value, char* string, int radix)
{
char tmp[33];
char* tp = tmp;
int i;
unsigned v;
int sign;
char* sp;
if (radix > 36 || radix <= 1)
{
__set_errno(EDOM);
return 0;
}
sign = (radix == 10 && value < 0);//1.如果是10进制,则要判断符号
if (sign)
v = -value;//2.如果为负,则取绝对值
else
v = (unsigned)value;
while (v || tp == tmp)//3.两个条件一个也不能少,因为v为0时也到得到0结果,而不能结束函数
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+''0'';//tp和结果的顺序正好相反
else
*tp++ = i + ''a'' - 10;
}
if (string == 0)//4.string为null则为string申请空间,大小为数字长度+符号+'\0';
string = (char*)malloc((tp-tmp)+sign+1);
sp = string;//5.将string赋给sp,以便返回
if (sign)//6.如果有符号,则加入
*sp++ = ''-'';
while (tp > tmp)//7.将tp反转,则正好是结果
*sp++ = *--tp;
*sp = 0;//8.string的末尾置为'\0'
return string;
}