`

字符串和数字之间的转换

 
阅读更多

#include<stdlib.h>

字符串转数字

int atoi(const char *nptr);

int atol(const char *nptr);==long int strtol(const char *nptr, char **endptr,int base);

long int strtol(const char *nptr, char **endptr,int base);base表示进制,endptr返回不符合条件而终止的字符的指针,出现非数字或字符串结束符终止,出现数字或正符号才开始转换。

double atof(const char *nptr);

double strtod(const char *nptr,char **endptr);

数字转字符串

char *gcvt(double number,size_t ndigits,char *buf);number转成字符串,放入buf所指向的缓冲区,ndigits为能显示的位数。

 

#include<stdio.h>
#include<stdlib.h>
int main(){
//int atoi,long  atol=long int strtol
char a[]="-100";
char b[]="0x20";
int c;
c=atoi(a)+atoi(b);
printf("c=%d\n",c);
//long int strtol(const char *nptr,const char **endptr,int base)
char two[]="10000";
char twelve[]="20000";
char sixteen[]="FFFF";
printf("two=%ld\n",strtol(two,NULL,2));
printf("twelve=%ld\n",strtol(twelve,NULL,2));
printf("sixteen=%ld\n",strtol(sixteen,NULL,2));
//double atof
char doub[]="188.888";
printf("double=%f\n",atof(doub));
//strtod
printf("strtod double=%f\n",strtod(doub,NULL));
//char *gcvt
double gcvtd=666.666;
char str[64];
gcvt(gcvtd,6,str);
printf("gcvt str:%s",str);
return 0;   
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics