现在的位置: 首页 > 综合 > 正文

编写函数实现:整型数转换成字符串

2012年10月06日 ⁄ 综合 ⁄ 共 555字 ⁄ 字号 评论关闭
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void int_to_str(unsigned int src,unsigned char* dest);

void main(void)
{
	unsigned int num = 0;
	unsigned char str[11] = {0};
	printf("scanf a number:\n");
	scanf("%d",&num);
	int_to_str(num,str);	
	printf("after convert: %s\n",str);
}

void int_to_str(unsigned int src,unsigned char* dest)
{
	char pos = 0;
	char len = 0;	
	if(NULL == dest)
		return ;
	do{
		dest[pos++] = src % 10 + '0';
	}while(src /= 10);
	dest[pos] = '\0';	
	len = strlen((const char*)dest);
	for(pos = 0;pos < len/2;pos++)
	{
		dest[pos] += dest[len-pos-1];
		dest[len-pos-1] = dest[pos]-dest[len-pos-1];
		dest[pos] -= dest[len-pos-1];
	}
}

抱歉!评论已关闭.