使用标准C自己编写一个函数,怎么将数据转化为字符串。

JWhiteHorse 2002-03-18 02:34:01
要求函数的返回值为字符串,能识别正负。
...全文
39 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
onlykiller 2002-03-18
  • 打赏
  • 举报
回复
混合体不是说了吗 itoa
wiler 2002-03-18
  • 打赏
  • 举报
回复
判别正负的如下:
char *ToString(double num)
{
char *str,*head;
double Decimal;
long Integer,m,n;

str=(char *)malloc(100);
head=str;
if(num<0){num=0.0-num;*(str++)='-';}
Decimal=num-(long)num;
Integer=(long)num;

if(Integer==0)
*(str++)='0';
while(Integer)
{
m=Integer/10;
n=Integer%10;
*(str++)=n+'0';
Integer=m;
}
if(Decimal>0)
{
*(str++)='.';
while(Decimal>0)
{
m=(long)Decimal*10;
Decimal=Decimal*10-m;
*(str++)=m+'0';
}
}
*str=NULL;
return head;
}
wiler 2002-03-18
  • 打赏
  • 举报
回复
char *ToString(double num)
{
char *str,*head;
double Decimal;
long Integer,m,n;

str=(char *)malloc(100);
head=str;
Decimal=num-(long)num;
Integer=(long)num;

if(Integer==0)
*(str++)='0';
while(Integer)
{
m=Integer/10;
n=Integer%10;
*(str++)=n+'0';
Integer=m;
}
if(Decimal>0)
{
*(str++)='.';
while(Decimal>0)
{
m=(long)Decimal*10;
Decimal=Decimal*10-m;
*(str++)=m+'0';
}
}
*str=NULL;
return head;
}
Cauty45 2002-03-18
  • 打赏
  • 举报
回复
向来一个int
int f( int n, char s[])
{
int p=0;

if (n<0){
s[p]='-';
p++;
n=-n;
}
if(n==0){
s[p++]='0';
s[p]='\0';
}
while(n>0){
s[p++]=n%10+'0';
n/=10;
}
s[p]='\0';

if(s[0]=='-'){
s++;
p--;
}

char c;
for(int i=0; i<p/2; i++){
c=s[i];
s[i]=s[p-i-1];
s[p-i-1]=c;
}
}
JWhiteHorse 2002-03-18
  • 打赏
  • 举报
回复
当然越全越好:)
Cauty45 2002-03-18
  • 打赏
  • 举报
回复
什么样的数字?
整数
浮点数(包括科学计数法吗)?
JWhiteHorse 2002-03-18
  • 打赏
  • 举报
回复
谁有好的方法,再加100分!
yeq 2002-03-18
  • 打赏
  • 举报
回复
如果是INT 型数据,根据 对应字符与数字ASCII码的差异转化。
在转化过程中,只要注意一点,在标准C中,字符与整型是通用的。
例如字符“1”的ASCII码是049,所以如果要把整型数据1转化为字符1
只需加48就行了。同样,数据2加48就变成了字符2
int a[9];
char ch[9]={123456123};
for (i=0;i<9;i++)
a[i]=ch[i]+48;
按照这样的方法,就把数字转化成字符了。
rockhard 2002-03-18
  • 打赏
  • 举报
回复
我以前在单片机编程时也遇到类似问题,当时是将整数与浮点分开考虑的处理的,代码不是很多,小数稍麻烦点,我先将它乘以1000,精确到小数点三位(一般的情况下够了),再调用整数到串的函数处理。如果想精确处理,得知道它具体的存储格式。具体可以查看相关资料。
prototype 2002-03-18
  • 打赏
  • 举报
回复
there is no portable way for writing such a function, because you have to know the underlying data structure for 'int', 'float', ..., which are mathine-dependent and not defined in standard c.
JWhiteHorse 2002-03-18
  • 打赏
  • 举报
回复
运用在单片机的编程中,没有提供以上函数。
自己写了一个觉得方法很苯。
tigerfox 2002-03-18
  • 打赏
  • 举报
回复
itoa
prototype 2002-03-18
  • 打赏
  • 举报
回复
sigh, no one would like to do such a meaningless thing (unless it is a homework required by a BT teacher)...
prototype 2002-03-18
  • 打赏
  • 举报
回复
sigh, no one would like to do such a meaningless thing (unless it is a homework required by a BT teacher)...
JWhiteHorse 2002-03-18
  • 打赏
  • 举报
回复
看看谁的算法最快最好。
JWhiteHorse 2002-03-18
  • 打赏
  • 举报
回复
要求,自己编写的一个函数:)
prototype 2002-03-18
  • 打赏
  • 举报
回复
if the number is a floating number, use (example):

sprintf( buffer, "%f", 1.2345 );

anyway, the formatting rules for 'sprintf" are the same as those for 'printf'
prototype 2002-03-18
  • 打赏
  • 举报
回复
faint, this has been asked several times today.

use 'sprintf', a standard function:
i write an example (again):

#include<stdio.h>

...
char buffer[1024];
sprintf( buffer, "%d", 12345 ); // buffer becomes "12345".


69,382

社区成员

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

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