3种整形转字符串的方法
#include <stdio.h>
typedef unsigned char Uint8;
typedef unsigned short Uint16;
typedef unsigned int Uint32;
static void IntToStr1(Uint32 num, Uint8 *const paucBuff)
{
int i = 0;
int j = 0;
char temp[13] = {0x00};
while(num)
{
temp[i] = num % 10 + 0x30 ;
num = num / 10;
i++;
}
temp[i] = 0;
i = i - 1;
while(i >= 0)
{
paucBuff[j] = temp[i];
i--;
j++;
}
paucBuff[j] = 0;
}
static void IntToStr2(Uint32 num, Uint8 *const paucBuff)
{
int i = 9;
while(num)
{
paucBuff[i] = num % 10 + 0x30 ;
num = num / 10;
i--;
}
paucBuff[10] = 0;
}
void main(void)
{
Uint32 i = 12;
Uint32 j = 123;
Uint32 k = 4294967295;
char aucBuf1[15] = {0x0};
char aucBuf2[15] = {0x0};
char aucBuf3[15] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
sprintf((char*)&aucBuf1[0], "%u", k);
IntToStr1(j, &aucBuf2[0]);
IntToStr2(i, &aucBuf3[0]);
puts(aucBuf1);
puts(aucBuf2);
puts(aucBuf3);
}
|