上代码之前先讲个笑话:曾经有位面试官问:“你实现过 唉踢哦诶(音) 吗”? 我第一个想到的是各种OA系统,心想那玩意不多是Java实现的吗。。。过一会想明白了,瞬间石化。。。
#include <cstdio>
#include <stdint.h> int8_t Myitoa(int64_t In_i64SrcNum, uint8_t In_i8Radix, char *In_pcDst)
{
int8_t i8RetVal = ;
//这里强转是为了2、8、16进制显示的时候不需要符号
uint64_t ui64SrcNum = (uint64_t)In_i64SrcNum;
char *pcDst = In_pcDst;
const char acTable[] = "0123456789ABCDEF"; if (In_i8Radix <= || pcDst == NULL)
{
i8RetVal = -;
goto fun_ret;
} if (ui64SrcNum == )
{
*(pcDst ++) = '';
*pcDst = ;
goto fun_ret;
} if ((int64_t)ui64SrcNum < && In_i8Radix == )
{
ui64SrcNum = ~ui64SrcNum + ;
*(pcDst ++) = '-';
} do //do...while循环的效率较高
{
*(pcDst ++) = acTable[ui64SrcNum % In_i8Radix];
ui64SrcNum /= In_i8Radix;
} while (ui64SrcNum);
*pcDst = ;
pcDst --; In_pcDst = *In_pcDst == '-' ? In_pcDst + : In_pcDst;
do
{
char cTmp = *In_pcDst;
*In_pcDst = *pcDst;
*pcDst = cTmp;
In_pcDst ++;
pcDst --;
} while (In_pcDst < pcDst); fun_ret:
return i8RetVal;
} void main()
{
int64_t i64Test = -;
char acBinStr[] = {};
char acOctStr[] = {};
char acDecStr[] = {};
char acHexStr[] = {};
Myitoa(i64Test, , acBinStr);
Myitoa(i64Test, , acOctStr);
Myitoa(i64Test, , acDecStr);
Myitoa(i64Test, , acHexStr);
printf("%s\n%s\n%s\n%s\n", acBinStr, acOctStr, acDecStr, acHexStr);
return;
}
趁着找工作这段时间,补补基础,唉,不是科班出身的短板啊。。。
参考:
http://www.cnblogs.com/applebunny/archive/2012/06/21/2557361.html