怎么将int型转换成char*(在线)

superway117 2003-01-08 05:57:33
怎么将int型转换成char*
...全文
13180 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
俞庆平 2003-01-08
  • 打赏
  • 举报
回复
superway117(superway):我想你的这个问题可以这样来问:
如int i = 20;如何将其转成字符串"20".这样大家就明白你的要求了。
如果是仅仅将i当作字符处理又是另外一回事。
即你说怎么将int型转换成char* 让很多人误解,以为你是要将int类型转型为char *类型。
superway117 2003-01-08
  • 打赏
  • 举报
回复
风吹吹风的方法挺好的,谢了
cjnet 2003-01-08
  • 打赏
  • 举报
回复
看一看强制类型转换的用法。
superway117 2003-01-08
  • 打赏
  • 举报
回复
我补充一下:我要得是 把一个int变为字符串
例int i=69
我想得到"69"
而实际上c语言中并没有itoa()函数,只有atoi()
shantian 2003-01-08
  • 打赏
  • 举报
回复
up
normalnotebook 2003-01-08
  • 打赏
  • 举报
回复
1.强制类型转换,(char *)int
2.用sprintf()转换成char型,再强制类型转换,(char *)char
metellica 2003-01-08
  • 打赏
  • 举报
回复 1
问题不是很明确
如:int i = 69;
你是要"69"还是“E"(E的ascii为69)
要"69":用itoa()
要"E":用char *p = (char *)&i;
sunjun240 2003-01-08
  • 打赏
  • 举报
回复
如果你一个函数需要char*类型?
你的意思是函数的返回值要转换成char*类型!
5956 2003-01-08
  • 打赏
  • 举报
回复
Header File

stdlib.h

Category

Conversion Routines, Math Routines

Prototype

char *itoa(int value, char *string, int radix);

wchar_t *_itow(int value, wchar_t *string, int radix);

Description

Converts an integer to a string.

itoa converts value to a null-terminated string and stores the result in string. With itoa, value is an integer. _itow is the unicode version of the function. It converts an integer to a wide-character string.

radix specifies the base to be used in converting value; it must be between 2 and 36, inclusive. If value is negative and radix is 10, the first character of string is the minus sign (-).

Note: The space allocated for string must be large enough to hold the returned string, including the terminating null character (\0). itoa can return up to 33 bytes.

Return Value

itoa returns a pointer to string.
metellica 2003-01-08
  • 打赏
  • 举报
回复
问题不是很明确
如:int i = 69;
你是要"69"还是“E"(E的ascii为69)
sunjun240 2003-01-08
  • 打赏
  • 举报
回复
学习!!
windcsn 2003-01-08
  • 打赏
  • 举报
回复
如果你一个函数需要char*类型,你提供的是int类型的要如此转换
(char *)&(整形变量)
metellica 2003-01-08
  • 打赏
  • 举报
回复
itoa(int value, char *string, int radix );


sunjun240 2003-01-08
  • 打赏
  • 举报
回复
一下上来两个!我看没人回答才!
惭愧
windcsn 2003-01-08
  • 打赏
  • 举报
回复
Polarislee(北极星)
的做法不行,str将是???
sunjun240 2003-01-08
  • 打赏
  • 举报
回复

1: int->char

#define HEXASCII(val) ((val)>0 && (val)<10)?'0'+(val):\
((val)>9 && (val)<16)?'A'+(val)-10:\
'0'

2: char ->char*

应该行吧!具体我也不太会!!!

windcsn 2003-01-08
  • 打赏
  • 举报
回复
char p[20];
int a = 20;
sprintf(p,"%d",a);
北极猩猩 2003-01-08
  • 打赏
  • 举报
回复 2
强制类型转换
char *str = (char*)i;

69,382

社区成员

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

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