C语言中怎么把Int型转成字符型?

deon 2002-01-29 12:09:26
C语言中怎么把Int型转成字符型?请给代码。
...全文
9335 54 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
54 条回复
切换为时间正序
请发表友善的回复…
发表回复
Snake_hu 2002-03-21
  • 打赏
  • 举报
回复
在c\c++中char於int存儲的是相同的。如char A相當於int 65.
int i= 65;
char c = i; //c= 'A'
pig_2000 2002-03-21
  • 打赏
  • 举报
回复
byte和char可以互相转换,int和char不行,必须8位8位的转换。
hyeena 2002-02-05
  • 打赏
  • 举报
回复
你要做什么,简单的类型转换还是就把int当作char来处理,
如果是后者,就用指针和位运算
zzbsw 2002-02-05
  • 打赏
  • 举报
回复 1
int i ;
char a;

a = *((char *)(&i));
a = *((char *)(&i) + 1);
vioy 2002-02-05
  • 打赏
  • 举报
回复
怎么把Int型转成字符型?
??
是把值转成字符(串),还是直接类型转换?
guixianren 2002-02-05
  • 打赏
  • 举报
回复
sourceforge.org的Code Snippet
的代码有一个小错误,n不应在计算中简单使用,而是应该用(unsigned)n

下面是我看到的一个比较好的例子
/* itoa.c is a portable version of Microsoft C's iota(). Unlike the Microsoft
version of the function, this version will return (along with the ASCII ver-
sion of the number) the "0x" prefix in the case of hexadecimal and the "0"
prefix in the case of octal. Honestly, sprintf() could probably be used just
as or more effectively than this program. In fact, the program uses
sprintf() to format the number as a string. Nevertheless, this program will
likely preclude the need to rewrite code if, for instance, the programmer
needs to port a DOS C program compiled by Microsoft C to a UNIX platform.
Note the program will use only 16-bit integers. This program was completed
on 8 Mar 93 by Christopher R. Gardner, Warner Robins Air Logistics Center.
*/

char *itoa(int n, char *s, int b) /* where b is the numerical base */
{
int c;

switch(b)
{
case 10: /* decimal */
sprintf(s, "%d", n);
break;
case 16: /* hexadecimal */
sprintf(s, "%#x", n);
break;
case 8: /* octal */
sprintf(s, "%#o", n);
break;
case 2: /* binary */
for (c = 16 - 1; c >= 0; c--, n >>= 1)
s[c] = (01 & n) + '0';
s[16] = '\0';
break;
default:
printf("Run Time Error: itoa() base argument not allowed\n");
exit(2);
} return s;
}


wl_whu 2002-02-05
  • 打赏
  • 举报
回复
有没有可以吧无符号型转换为char;
我试了IntToStr()不行。
weixiao 2002-02-04
  • 打赏
  • 举报
回复
itoa()
atoi()
等等都是用来转换类型的:)
fishlovewater 2002-02-02
  • 打赏
  • 举报
回复
对的,楼上的我同意,本来就是通的嘛,用不作很复杂
csdnleon 2002-02-02
  • 打赏
  • 举报
回复
不用转化,直接这样:printf("%c",整形数);
liutiejiang 2002-02-02
  • 打赏
  • 举报
回复
C++中,
int i=100;
char temp[20];
wsprintf(temp,"%d",i);
//此时,就把int型转化为字符型了
elc 2002-02-02
  • 打赏
  • 举报
回复
_itoa(int i, char *s , int d)
fishlovewater 2002-02-02
  • 打赏
  • 举报
回复
我举个例:
main()
{int a=98;
printf("%c",a);}结果就OK了噻
poorczz 2002-02-01
  • 打赏
  • 举报
回复
太简单了只需在printf中用%c
bigcan 2002-02-01
  • 打赏
  • 举报
回复
IntToStr只是c++ builder才有的吧,不是標準庫的!
igame 2002-02-01
  • 打赏
  • 举报
回复
字符型和整型有什么不同?
不就是一个八位,另一个16或32位吗?
除了在表示精度上,计算偏移上有不同外,看不出有什么不同


wlan1133 2002-02-01
  • 打赏
  • 举报
回复
可以相互使用,不必转换。但要注意字符的ASCLL码的范围。
heimeng 2002-02-01
  • 打赏
  • 举报
回复
回复人: pp7(不才) (2002-1-29 0:31:52) 得0分
据我所知,C/C++中字符就是以int的形式存在的。

“ 将一个字符常量赋值给字符变量,实际上并不是把该字符本身放到
内存单元中,而是将该字符的相应ASCII码(整型数)存入。例如,字符
‘a'的ASCII码是97,上例中“c5=97”即为“c5='a'”。
“ 在内存中,字符数据以ASCII码存储,即以整数表示(原文这一句
为黑体字),所以C++中字符数据和整型数据之间可以相互赋值,只要
注意其表示的范围合理。”

-----------摘自《C++程序设计教程》 钱能
cgb405 2002-02-01
  • 打赏
  • 举报
回复
这么复杂,我想应该很简单吧!!
直接用(char)变量就行了!!
leibo 2002-02-01
  • 打赏
  • 举报
回复
老大们???用c-run time lib好吗?::itoa()<<->>查msdn
加载更多回复(34)

15,447

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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