#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
/* 实现itoa函数 int to array */
char* _itoa(int value, char* string, int radix)
{
if(radix < 0 or radix >36)
{
cout <<"radix is invaild,return "<<endl;
return 0;
}
int sign = (radix == 10 and value < 0);
int tmpValue;
if(sign)
{
tmpValue = - value;
}
else
{
tmpValue = (unsigned)value;
}
char tmpChar[33];
char* tmpResult = tmpChar;
int tmp;
while(tmpValue or tmpResult == tmpChar)
{
tmp = tmpValue % radix;
tmpValue = tmpValue / radix;
if(tmp < 10)
{
*tmpResult++ = tmp + '0';
}
else
{
*tmpResult++ = tmp + 'a' - 10;
}
}
if(string == 0)
{
string = (char*)malloc(tmpResult - tmpChar + sign + 1);
}
char *tmpString = string;
if(sign)
{
*tmpString++ = '-';
}
while(tmpResult > tmpChar)
{
*tmpString++ = *--tmpResult;
}
tmpString = 0;
return string;
}
int main()
{
int number = 0xabcd;
char string[25];
_itoa(number, string, 54);
printf("integer = %x string = %s\n", number, string);
cout <<std::to_string(number) <<endl;
std::
getchar();
return 0;
}