函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/

atof(将字符串转换成浮点型数)
相关函数
atoi,atol,strtod,strtol,strtoul
表头文件
#include <stdlib.h>
定义函数
double atof(const char *nptr);
函数说明
atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值
返回转换后的浮点型数。
附加说明
atof()与使用strtod(nptr,(char**)NULL)结果相同。
范例
/* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char *a=”-100.23”;
char *b=”200e-2”;
float c;
c=atof(a)+atof(b);
printf(“c=%.2f\n”,c);
}
执行
c=-98.23
 

atoi(将字符串转换成整型数)
相关函数
atof,atol,atrtod,strtol,strtoul
表头文件
#include<stdlib.h>
定义函数
int atoi(const char *nptr);
函数说明
atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
返回值
返回转换后的整型数。
附加说明
atoi()与使用strtol(nptr,(char**)NULL,10);结果相同。
范例
/* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
mian()
{
char a[]=”-100”;
char b[]=”456”;
int c;
c=atoi(a)+atoi(b);
printf(c=%d\n”,c);
}
执行
c=356
 

atol(将字符串转换成长整型数)
相关函数
atof,atoi,strtod,strtol,strtoul
表头文件
#include<stdlib.h>
定义函数
long atol(const char *nptr);
函数说明
atol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
返回值
返回转换后的长整型数。
附加说明
atol()与使用strtol(nptr,(char**)NULL,10);结果相同。
范例
/*将字符串a与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=” 234567890”;
long c;
c=atol(a)+atol(b);
printf(“c=%d\n”,c);
}
执行
c=1234567890
 

gcvt(将浮点型数转换为字符串,取四舍五入)
相关函数
ecvt,fcvt,sprintf
表头文件
#include<stdlib.h>
定义函数
char *gcvt(double number,size_t ndigits,char *buf);
函数说明
gcvt()用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。gcvt()与ecvt()和fcvt()不同的地方在于,gcvt()所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。
返回值
返回一字符串指针,此地址即为buf指针。
附加说明
范例
#include<stdlib.h>
main()
{
double a=123.45;
double b=-1234.56;
char *ptr;
int decpt,sign;
gcvt(a,5,ptr);
printf(“a value=%s\n”,ptr);
ptr=gcvt(b,6,ptr);
printf(“b value=%s\n”,ptr);
}
执行
a value=123.45
b value=-1234.56
 

strtod(将字符串转换成浮点数)
相关函数
atoi,atol,strtod,strtol,strtoul
表头文件
#include<stdlib.h>
定义函数
double strtod(const char *nptr,char **endptr);
函数说明
strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('\0')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。
返回值
返回转换后的浮点型数。
附加说明
参考atof()。
范例
/*将字符串a,b,c 分别采用10,2,16 进制转换成数字*/
#include<stdlib.h>
mian()
{
char a[]=”1000000000”;
char b[]=”1000000000”;
char c[]=”ffff”;
printf(“a=%d\n”,strtod(a,NULL,10));
printf(“b=%d\n”,strtod(b,NULL,2));
printf(“c=%d\n”,strtod(c,NULL,16));
}
执行
a=1000000000
b=512
c=65535
 

strtol(将字符串转换成长整型数)
相关函数
atof,atoi,atol,strtod,strtoul
表头文件
#include<stdlib.h>
定义函数
long int strtol(const char *nptr,char **endptr,int base);
函数说明
strtol()会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
返回值
返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明
ERANGE指定的转换字符串超出合法范围。
范例
/* 将字符串a,b,c 分别采用10,2,16进制转换成数字*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=”1000000000”;
char c[]=”ffff”;
printf(“a=%d\n”,strtol(a,NULL,10));
printf(“b=%d\n”,strtol(b,NULL,2));
printf(“c=%d\n”,strtol(c,NULL,16));
}
执行
a=1000000000
b=512
c=65535
 

strtoul(将字符串转换成无符号长整型数)
相关函数
atof,atoi,atol,strtod,strtol
表头文件
#include<stdlib.h>
定义函数
unsigned long int strtoul(const char *nptr,char **endptr,int base);
函数说明
strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
返回值
返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明
ERANGE指定的转换字符串超出合法范围。
范例
参考strtol()
 

toascii(将整型数转换成合法的ASCII 码字符)
相关函数
isascii,toupper,tolower
表头文件
#include<ctype.h>
定义函数
int toascii(int c)
函数说明
toascii()会将参数c转换成7位的unsigned char值,第八位则会被清除,此字符即会被转成ASCII码字符。
返回值
将转换成功的ASCII码字符值返回。
范例
#include<stdlib.h>
main()
{
int a=217;
char b;
printf(“before toascii () : a value =%d(%c)\n”,a,a);
b=toascii(a);
printf(“after toascii() : a value =%d(%c)\n”,b,b);
}
执行
before toascii() : a value =217()
after toascii() : a value =89(Y)
 

tolower(将大写字母转换成小写字母)
相关函数
isalpha,toupper
表头文件
#include<stdlib.h>
定义函数
int tolower(int c);
函数说明
若参数c为大写字母则将该对应的小写字母返回。
返回值
返回转换后的小写字母,若不须转换则将参数c值返回。
附加说明
范例
/* 将s字符串内的大写字母转换成小写字母*/
#include<ctype.h>
main()
{
char s[]=”aBcDeFgH12345;!#$”;
int i;
printf(“before tolower() : %s\n”,s);
for(i=0;I<sizeof(s);i++)
s[i]=tolower(s[i]);
printf(“after tolower() : %s\n”,s);
}
执行
before tolower() : aBcDeFgH12345;!#$
after tolower() : abcdefgh12345;!#$
 

toupper(将小写字母转换成大写字母)
相关函数
isalpha,tolower
表头文件
#include<ctype.h>
定义函数
int toupper(int c);
函数说明
若参数c为小写字母则将该对映的大写字母返回。
返回值
返回转换后的大写字母,若不须转换则将参数c值返回。
附加说明
范例
/* 将s字符串内的小写字母转换成大写字母*/
#include<ctype.h>
main()
{
char s[]=”aBcDeFgH12345;!#$”;
int i;
printf(“before toupper() : %s\n”,s);
for(i=0;I<sizeof(s);i++)
s[i]=toupper(s[i]);
printf(“after toupper() : %s\n”,s);
}
执行
before toupper() : aBcDeFgH12345;!#$
after toupper() : ABCDEFGH12345;!#$

linux实例代码如下:

  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. void atof_fun();//string reverse to float
  6.  
  7. void atoi_fun();//string reverse to int
  8.  
  9. void atol_fun();//string reverse to long int
  10.  
  11. void gcvt_fun();//float reverse to string
  12.  
  13. void strtod_fun();//string reverse to float
  14.  
  15. void strtol_fun();//string reverse to long int
  16.  
  17. void toascii_fun();//int reverse to ascii
  18.  
  19. void tolower_fun();//such as A reverse a;
  20.  
  21. void toupper_fun();//such as a reverse A;
  22.  
  23. int main()
  24.  
  25. {
  26.  
  27. int Index;
  28.  
  29. printf("1.atof---string reverse to float\n");
  30.  
  31. printf("2.atoi---string reverse to int\n");
  32.  
  33. printf("3.atol---string reverse to long int\n");
  34.  
  35. printf("4.gcvt---float reverse to string\n");
  36.  
  37. printf("5.strtod---string reverse to float\n");
  38.  
  39. printf("6.strtol---string reverse to long int\n");
  40.  
  41. printf("7.toascii---int reverse to ascii\n");
  42.  
  43. printf("8.tolower---such as A reverse to a\n");
  44.  
  45. printf("9.toupper---such as a reverse to A\n");
  46.  
  47. printf("0.Exit\n");
  48.  
  49. while()
  50.  
  51. {
  52.  
  53. printf("Please choose the function you want done(0-exit):\n");
  54.  
  55. scanf("%d",&Index);
  56.  
  57. if(Index==)
  58.  
  59. {
  60.  
  61. printf("Bye Bye\n");
  62.  
  63. break;
  64.  
  65. }
  66.  
  67. else
  68.  
  69. {
  70.  
  71. switch(Index)
  72.  
  73. {
  74.  
  75. case :atof_fun();
  76.  
  77. break;
  78.  
  79. case :atoi_fun();
  80.  
  81. break;
  82.  
  83. case :atol_fun();
  84.  
  85. break;
  86.  
  87. case :gcvt_fun();
  88.  
  89. break;
  90.  
  91. case :strtod_fun();
  92.  
  93. break;
  94.  
  95. case :strtol_fun();
  96.  
  97. break;
  98.  
  99. case :toascii_fun();
  100.  
  101. break;
  102.  
  103. case :tolower_fun();
  104.  
  105. break;
  106.  
  107. case :toupper_fun();
  108.  
  109. break;
  110.  
  111. default:
  112.  
  113. break;
  114.  
  115. }
  116.  
  117. }
  118.  
  119. }
  120.  
  121. return ;
  122.  
  123. }
  124.  
  125. //string reverse to float
  126.  
  127. void atof_fun()
  128.  
  129. {
  130.  
  131. char a[];
  132.  
  133. char b[];
  134.  
  135. printf("Please input the string a:\n");
  136.  
  137. scanf("%s",a);
  138.  
  139. printf("Please input the string b:\n");
  140.  
  141. scanf("%s",b);
  142.  
  143. float c;
  144.  
  145. c=atof(a)+atof(b);
  146.  
  147. printf("float number c=%.2f\n",c);
  148.  
  149. }
  150.  
  151. //string reverse to int
  152.  
  153. void atoi_fun()
  154.  
  155. {
  156.  
  157. char a[];
  158.  
  159. char b[];
  160.  
  161. printf("Please input the string a:\n");
  162.  
  163. scanf("%s",a);
  164.  
  165. printf("Please input the string b:\n");
  166.  
  167. scanf("%s",b);
  168.  
  169. int c;
  170.  
  171. c=atoi(a)+atoi(b);
  172.  
  173. printf("int number c=%d\n",c);
  174. }
  175.  
  176. //string reverse to long int
  177.  
  178. void atol_fun()
  179.  
  180. {
  181.  
  182. char a[];
  183.  
  184. char b[];
  185.  
  186. printf("Please input the string a:\n");
  187.  
  188. scanf("%s",a);
  189.  
  190. printf("Please input the string b:\n");
  191.  
  192. scanf("%s",b);
  193.  
  194. long c;
  195.  
  196. c=atol(a)+atol(b);
  197.  
  198. printf("long int number c=%ld\n",c);
  199.  
  200. }
  201.  
  202. //float reverse to string
  203.  
  204. void gcvt_fun()
  205.  
  206. {
  207.  
  208. double a;
  209.  
  210. int aIndex;
  211.  
  212. double b;
  213.  
  214. int bIndex;
  215.  
  216. printf("Please input a and aIndex:\n");
  217.  
  218. scanf("%lf,%d",&a,&aIndex);
  219.  
  220. printf("Please input b and bIndex:\n");
  221.  
  222. scanf("%lf,%d",&b,&bIndex);
  223.  
  224. char str[];
  225.  
  226. gcvt(a,aIndex,str);
  227.  
  228. printf("%lf reverse to string---%s\n",a,str);
  229.  
  230. gcvt(b,bIndex,str);
  231.  
  232. printf("%lf reverse to string---%s\n",b,str);
  233.  
  234. }
  235.  
  236. //string reverse to float
  237.  
  238. void strtod_fun()
  239.  
  240. {
  241.  
  242. char a[],*ptr;
  243.  
  244. double value;
  245.  
  246. printf("Please input a string:\n");
  247.  
  248. scanf("%s",a);
  249.  
  250. value=strtod(a,&ptr);
  251.  
  252. printf("The string is %s the number is %lf\n",a,value);
  253.  
  254. }
  255.  
  256. //string reverse to long int
  257.  
  258. void strtol_fun()
  259.  
  260. {
  261.  
  262. char a[],*ptr;
  263.  
  264. long int value;
  265.  
  266. printf("Please input a string\n");
  267.  
  268. scanf("%s",a);
  269.  
  270. value=strtol(a,&ptr,);
  271.  
  272. printf("The string is %s the number is %ld\n",a,value);
  273.  
  274. }
  275.  
  276. //int reverse to ascii
  277. void toascii_fun()
  278.  
  279. {
  280.  
  281. int a;
  282.  
  283. char b;
  284.  
  285. printf("Please input the non-ascii number a:\n");
  286.  
  287. scanf("%d",&a);
  288.  
  289. b=toascii(a);
  290.  
  291. printf("Before ascii---(%d)to(%c)\n",a,a);
  292.  
  293. printf("After ascii---(%d)to(%c)\n",a,b);
  294.  
  295. }
  296.  
  297. //A reverse to a
  298.  
  299. void tolower_fun()
  300.  
  301. {
  302. char a[];
  303.  
  304. int i;
  305.  
  306. printf("Please input the string of a:\n");
  307.  
  308. scanf("%s",a);
  309.  
  310. printf("Before tolower:%s\n",a);
  311.  
  312. for(i=;a[i]!=;i++)
  313.  
  314. {
  315.  
  316. a[i]=tolower(a[i]);
  317.  
  318. }
  319.  
  320. printf("After tolower:%s\n",a);
  321.  
  322. }
  323.  
  324. //a reverse to A
  325.  
  326. void toupper_fun()
  327.  
  328. {
  329. char a[];
  330.  
  331. int i;
  332.  
  333. printf("Please input the string of a:\n");
  334.  
  335. scanf("%s",a);
  336.  
  337. printf("Before toupper:%s\n",a);
  338.  
  339. for(i=;a[i]!=;i++)
  340.  
  341. {
  342.  
  343. a[i]=toupper(a[i]);
  344.  
  345. }
  346.  
  347. printf("After toupper:%s\n",a);
  348.  
  349. }


运行结果:

 

Linux常用C函数---字符串转换篇的更多相关文章

  1. Linux常用C函数-接口处理篇(网络通信函数)

    接口处理篇accept,bind,connect,endprotoent,endservent,getsockopt,htonl,htons,inet_addr,inet_aton,inet_ntoa ...

  2. Linux常用C函数---字符测试篇

    函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/ isalnum(测试字符是否为英文或数字) 相关函数 isalpha,isdigit,islower,isupp ...

  3. Linux常用C函数---内存控制篇

    函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/ calloc(配置内存空间) 相关函数 malloc,free,realloc,brk 表头文件 #includ ...

  4. linux常用C函数目录

    字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...

  5. Linux常用系统函数

    Linux常用系统函数 一.进程控制 fork 创建一个新进程clone 按指定条件创建子进程execve 运行可执行文件exit 中止进程_exit 立即中止当前进程getdtablesize 进程 ...

  6. linux c/c++ IP字符串转换成可比较大小的数字

    由www.169it.com搜集整理 IP字符串转换成可比较大小的数字,具体代码如下所示: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include "stdio. ...

  7. ylb:SQLServer常用系统函数-字符串函数、配置函数、系统统计函数

    原文:ylb:SQLServer常用系统函数-字符串函数.配置函数.系统统计函数 ylbtech-SQL Server:SQL Server-SQLServer常用系统函数 -- ========== ...

  8. linux内核里的字符串转换 ,链表操作常用函数(转)

    1.对双向链表的具体操作如下: list_add ———向链表添加一个条目 list_add_tail ———添加一个条目到链表尾部 __list_del_entry ———从链表中删除相应的条目 l ...

  9. 归纳整理Linux下C语言常用的库函数----字符串转换、字符测试、及内存控制

    在没有IDE的时候,记住一些常用的库函数的函数名.参数.基本用法及注意事项是很有必要的. 参照Linux_C_HS.chm的目录,我大致将常用的函数分为一下几类: 1. 内存及字符串控制及操作 2. ...

随机推荐

  1. 【剑指Offer学习】【面试题60:把二叉树打印出多行】

    题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层打印一行. 解题思路 用一个队列来保存将要打印的结点.为了把二叉树的每一行单独打印到一行里,我们须要两个变量:一个变量表示在当前的 ...

  2. HDU 4978 A simple probability problem

    A simple probability problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  3. Linux shell 的 test 命令用法详解

    基本格式:test expression expression为test命令构造的表达式.这里expression是test命令可以理解的任何有效表达式,该简化格式将是读者可能会踫见的最常用格式返回值 ...

  4. 关于使用Html5 canvas、 map、jquery构造不规则变色点击区域 热点区域

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. ScriptManager需要用到的JS

    <script type="text/javascript"> Sys.Application.add_load(function() { var form = Sys ...

  6. sql server数据同步方案-日志传送

    1 功能描述 本方案采用日志传送模式,把核心数据库(主数据库)定期同步到灾备数据库(辅助服务器)及备份库(辅助服务器,便于其他系统使用,减轻主数据压力),期间,如果发生异常导致无法同步,将以电子邮件. ...

  7. 原生的UITableViewCell高度自适应,textLabel自动换行显示

    /* * 设置子项cell **/ - (UITableViewCell *)getChildCell:(UITableView *)tableView and:(NSIndexPath *)inde ...

  8. jQuery.on() 函数详解

    on() 函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于 ...

  9. (原)JNI中env->GetByteArrayElements和AndroidBitmap_getInfo的冲突

    也不是很确定,前段时间的代码没有出问题,但是今天调试了半天,一直崩溃:vm aborting. 以前的部分代码: JNIEXPORT void JNICALL XXX (JNIEnv* env,job ...

  10. Go语言中怎样判断数据类型_不懂的木匠_新浪博客

    要判断数据类型,可以用Go的空接口: 建一个函数t 设置参数i 的类型为空接口,空接口可以接受任何数据类型 func t(i interface{}) {  //函数t有一个参数i  switch i ...