首页 > 代码库 > 自定义整形转字符串 ltos

自定义整形转字符串 ltos


//dest 字符串存放  val 要转换的数据 base: 10 进制 16 16进制 

char * ltos(char *dest, ULONG val, ULONG base)

{

char tmp[16] = {0};

char *rtn = dest;

char ph[11] = {"0x00000000"};   // 这个是根据需要

int i = 0, j=0;

const char small_digits[] = "0123456789abcdef";


if (val == 0)

{

tmp[i++] = ‘0‘;

}

else

{

while(val != 0)

{

tmp[i++] = small_digits[val % base];

val /= base;

}

}

if (base == 16)

{

memcpy(dest, ph, 11 - i);

j += (10 - i);

}


while((dest[j++] = tmp[--i]) != ‘\0‘ && i);

return rtn;

}


自定义整形转字符串 ltos