不调用标准库函数,如何实现将INT类型转换成char类型输出

slwj 2011-08-01 11:39:21
问下,不调用标准库函数,如何实现将INT类型转换成char类型输出

多谢了!
...全文
170 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xunxun 2011-08-01
  • 打赏
  • 举报
回复
除了以上的,lz的输出需不需要使用标准库?
ryfdizuo 2011-08-01
  • 打赏
  • 举报
回复
#include <stdio.h>  
#include <stdlib.h>

int main(void)
{
int n,k,i,j,m=0;
char str[38] = {0};

printf("请输入要转换的10进制数n和要转换的进制k:");
scanf("%d%d",&n,&k);
while(n)
{
str[m++] = (n%k)["0123456789ABCDEF"];
n/=k;
}
str[m]='\0';
printf("转换前为:%s\n",str);
for(i=0,j=m-1;i<j; str[i]^=str[j]^=str[i]^=str[j], i++,j--);

printf("转换后为:%s\n",str);
system("PAUSE");
return 0;
}

请输入要转换的10进制数n和要转换的进制k:125 10
转换前为:521
转换后为:125
请按任意键继续. . .
ForestDB 2011-08-01
  • 打赏
  • 举报
回复
只有顶了。
icemornings 2011-08-01
  • 打赏
  • 举报
回复
摘自《C程序设计语言》

/* 反转字符串 */
void reverse(char s[])
{
int c, i, j;

for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

/* 转换int到char[] */
void itoa(int n, char s[])
{
int i, sign;

/* 记录符号位 */
if ((sign = n) < 0)
n = -n;
i = 0;

/* 把个位、十位……依次存到s[]中 */
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);

/* 把符号位存到最后 */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';

/* 反转字符串 */
reverse(s);
}

luciferisnotsatan 2011-08-01
  • 打赏
  • 举报
回复
10进制,%10加0x30。再除10。循环
16进,更简单。
G_8519 2011-08-01
  • 打赏
  • 举报
回复

char s[10] = {0};
int n = 0;
int Int = 65536;

do
{
s[n++] = (Int % 10) + '0';

} while ( Int /= 10 );
n--;
for ( int i = 0; i < n; i++, n-- )
{
char t = s[n];
s[n] = s[i];
s[i] = t;
}

printf("%s\n",s);
昵称很不好取 2011-08-01
  • 打赏
  • 举报
回复
使用位的与运算来截取吧
Michael_Xie 2011-08-01
  • 打赏
  • 举报
回复
int i = 65;
printf("%c\n", (char)i);
c_losed 2011-08-01
  • 打赏
  • 举报
回复
itoa源码么

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧