在编程中经常需要用到数字与字符串的转换,下面就总结一下。

1.atoi()

  C/C++标准库函数,用于字符串到整数的转换。

  函数原型:int atoi (const char * str);

 #include <stdio.h>
#include <stdlib.h>
int main ()
{
char *numchars="";
int num=atoi(numchars);
printf("%d\n",num);
return ;
}

  另外C/C++还提供的标准库函数有:

  (1)long int atol ( const char * str );  

  (2)double atof (const char* str);

2.itoa()

  不是C/C++标准库函数,用于整数到字符串的转换。

  函数原型:char *itoa(int value, char *string, int radix);

 #include <stdio.h>
#include <stdlib.h>
int main ()
{
int num=;
int radix=;
char res[];
itoa(num,res,radix);
printf("%d(10)=%s(%d)\n",num,res,radix); //输出:1234(10)=2322(8)
return ;
}

3.sprintf()

  C/C++标准库函数,可以用于整数到字符串的转换。

  sprintf:Write formatted data to string。

  sprintf作用是将printf的输出结果保存在字符串数组中。

 #include <stdio.h>
#include <stdlib.h>
int main ()
{
int num=;
char res[];
sprintf(res,"%0o",num);
printf("%s\n",res); //8进制输出:2322 sprintf(res,"%0x",num);
printf("%s\n",res); //16进制输出:4d2
return ;
}

  

C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数的更多相关文章

  1. 5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)

     1  多行函数(理解:有多个输入,但仅仅输出1个结果) SQL>select count(*) from emp; COUNT(*) ------------- 14 B 字符函数Lowe ...

  2. JavaScript学习笔记3之 数组 & arguments(参数对象)& 数字和字符串转换 & innerText/innerHTML & 鼠标事件

    一.Array数组 1.数组初始化(Array属于对象类型) /*关于数组的初始化*/ //1.创建 Array 对象--方法1: var arr1=[]; arr1[0]='aa';//给数组元素赋 ...

  3. C语言---整型字符串转换

    C语言提供了几个标准库函数,能够将随意类型(整型.长整型.浮点型等)的数字转换为字符串.下面是用itoa()函数将整数转 换为字符串的一个样例: # include <stdio.h>   ...

  4. python 数字和字符串转换问题

    一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) ( ...

  5. JavaScript数字和字符串转换示例

    http://www.jb51.net/article/48465.htm 1. 数字转换为字符串 a. 要把一个数字转换为字符串,只要给它添加一个空的字符串即可: 复制代码代码如下: var n = ...

  6. python学习第二天:数字与字符串转换及逻辑值

    1.数字与字符串的转化     #1.数字转字符,使用格式化字符串:         *1.demo = ‘%d’  %  source         *2.%d整型:%f 浮点型 :%e科学计数 ...

  7. cmd命令行进入DOS方式编译运行C语言程序实现字符串转换

    需求:输入一个字符串(长度小于50),然后过滤掉所有的非数字字符,得到由数字字符组成的字符串,将其转化为double型结果输出(4位小数). 源程序: #include<stdio.h>i ...

  8. C语言中将数字转换为字符串的方法

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio. h># ...

  9. js字符串转换成数字与数字转换成字符串的实现方法

    转载:点击查看地址 js字符串转换成数字 将字符串转换成数字,得用到parseInt函数.parseInt(string) : 函数从string的开始解析,返回一个整数. 举例:parseInt(' ...

随机推荐

  1. python import 与 from ... import ...

    import test test = 'test.py all code' from test import m1 m1 ='code'

  2. NetworkX 使用(二)

    官方教程 博客:NetworkX %pylab inline import networkx as nx Populating the interactive namespace from numpy ...

  3. 通过GeneXus如何快速构建微服务架构

    概览 “微服务”是一个非常广泛的话题,在过去几年里,市面上存在着各种不同的定义. 虽然对这种架构方式没有一个非常精确的定义,但仍然有一些概念具有代表性. 微服务有着许多围绕业务能力.自动化部署.终端智 ...

  4. android manifest.xml 文件

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 清单文件 包括 包名 应用 各个组件  四大组件 使用到的权限 应用程序所需要的最低安卓 ...

  5. Splay和LCT的复杂度分析

    \(Splay\)的复杂度分析 不论插入,删除还是访问,我们可以发现它们的复杂度都和\(splay\)操作的复杂度同阶,只是一点常数的区别 我们不妨假设有\(n\)个点的\(splay\),进行了\( ...

  6. bzoj 2460 拟阵+判线性相关

    /************************************************************** Problem: 2460 User: idy002 Language: ...

  7. python数据库操作——sqlite3模块

    # -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 ''' import sqlit ...

  8. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划

    A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...

  9. HDU 5683 zxa and xor 暴力模拟

    zxa and xor 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5683 Description zxa had a great interes ...

  10. C#高级编程9-第8章 委托、lamdba表达式和事件

    委托.lamdba表达式和事件 1.引用方法 函数指针是一个指向内存位置的指针,不是类型安全的.无法判断实际指向.参数和返回类型也无从知晓..NET委托是类型安全的.定义了返回类型和参数类型,不仅包含 ...