C++字符串转数字,数字转字符串
1. 字符串转数字
如将“32”转为32,将“3.1415”转为3.1415,将“567283”转为567283。使用:
//Convert string to integer, more @http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
int atoi ( const char * str );
//Convert string to double, more @http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
double atof ( const char * str );
// Convert string to long integer,more @http://www.cplusplus.com/reference/clibrary/cstdlib/atol/
long int atol ( const char * str );
//Read formatted data from string, more @http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
int sscanf ( const char * str, const char * format, ...);
Example1:
/* atoi,atof,atol example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a;float b;long c;
a=atoi("32");
b=atof("3.1415");
c=atol("567283");
printf ("%d,%f,%d",a,b,c);
return 0;
} Output:
32,3.141500,567283
Example2:
/* sscanf example */
#include <stdio.h>
int main ()
{
int a;float b;long c;
sscanf ("32,3.1415,567283","%d,%f,%d",&a,&b,&c);
printf ("%d,%f,%d",a,b,c);
return 0;
} Output:
32,3.141500,567283
2. 数字转字符串
如将32转为“32”,将3.1415转为“3.1415”,将567283转为“567283”。使用:
//Write formatted data to string, more @http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
int sprintf ( char * str, const char * format, ... );
//Convert integer to string (non-standard function), more @http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
//This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
//A standard-compliant alternative for some cases may be sprintf.
char * itoa ( int value, char * str, int base );
Example3:
Example3 /* sprintf example */
#include <stdio.h>
int main ()
{
char a[10],b[10],c[10];
sprintf(a, "%d", 32);
sprintf(b, "%f", 3.1415);
sprintf(c, "%d", 567283);
printf("%s,%s,%s",a,b,c);
return 0;
} Output:
32,3.141500,567283
3. 使用stringstream进行字符串与数字的转换
这里给出一个例子,更多 @http://www.cppblog.com/Sandywin/archive/2008/08/27/27984.html,http://blog.csdn.net/touzani/article/details/1623850
Example4:
Example4 // using stringstream constructors.
#include <iostream>
#include <sstream>
using namespace std;
int main ()
{
int a;float b;long c;
char d[10],e[10],f[10]; /* string to number */
stringstream ss;
ss << "32";
ss >> a;
ss.clear(); ss << "3.1415";
ss >> b;
ss.clear(); ss << "567283";
ss >> c;
ss.clear(); cout<<a<<","<<b<<","<<c<<endl; /* number to string */
ss << a;
ss >> d;
ss.clear(); ss << b;
ss >> e;
ss.clear(); ss << c;
ss >> f;
ss.clear(); cout<<d<<","<<e<<","<<f<<endl; return 0;
} Output:
32,3.141500,567283
32,3.141500,567283
小结:C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性,可以使用这些库来实现安全和自动的类型转换。言下之意就是,使用stringstream进行字符串与数字的转换是更好的选择。
C++字符串转数字,数字转字符串的更多相关文章
- 写出将字符串中的数字转换为整型的方法,如:“as31d2v”->312,并写出相应的单元测试,正则去掉非数值、小数点及正负号外的字符串
写出将字符串中的数字转换为整型的方法,如:"as31d2v"->312,并写出相应的单元测试,输入超过int范围时提示不合法输入. public struct Convert ...
- Python 判断字符串是否为数字
转载: http://www.runoob.com/python3/python3-check-is-number.html 以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为 ...
- Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字
不说废话,贴代码: CREATE OR REPLACE FUNCTION to_time(sec IN NUMBER) RETURN VARCHAR2 IS /*把秒转成时分秒格式 auth lzpo ...
- C#判断字符串是否是数字
/// <summary> /// 判断字符串是否是数字 /// </summary> public static bool IsNumber(string s) { if ( ...
- Java--正则表达式-简单的在字符串中找数字
import org.junit.Test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...
- SQL Server 2008 R2——创建函数 筛选出字符串中的数字 筛选出字符串中的非数字
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- Asp.net,C# 纯数字加密解密字符串
也就是说加密后的数据不再是:N8lAaHMFtSAQgaf3+RUFng== 希望encryptedString是"1203877893704809384098328409234923840 ...
- 将字符串转化为数字(Convert和Parse的用法)
字符串必须是数字,不要超过转换成目标数字类型的范围.超过的话系统也会报错(溢出). static void Main(string[] args) { string s; int i; Console ...
- 验证一个字符串是否由数字组成(Java)
public class StringDemo{ public static void main(String args[]){ String str ="12343264sd6223&qu ...
- Excel中如何提取字符串中的数字
取字符串中的数字,假如数据在A列,提取公式为 =LOOKUP(9^9,--MID(A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&5^19)),ROW($1:$99) ...
随机推荐
- Java学习经验
随着Java学习的深入,越来越感觉记笔记的重要性,一方面可以使自己更加善于总结,提高对项目和自己的认知,另一方面可以让知识条例更加鲜明,并且加深对知识点的记忆.Java是一门很早开始兴起的语言,用途非 ...
- 如何用纯 CSS 创作一个均衡器 loader 动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/oybWBy 可交互视频教 ...
- tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04
http://blog.csdn.net/liudijiang/article/details/50555429(转) 首先得要有制作好的uboot和linux内核镜像zImage,先烧录到sd卡里, ...
- One-to-one
创建模型 在本例中,Place 和 Restaurant 为一对一关系 from django.db import models class Place(models.Model): name = m ...
- 不同深度的图片转换cvConvertScale
不同深度图像的转换:要注意范围比如IPL_DEPTH_8U 转到 IPL_DEPTH_32U要用cvConvertScale(pImg8, pImg32, 1.0/255, 0); 要除255反过来I ...
- git commit 含有中文的代码,提示Warnning:Your console font probably doesn't support Unicode.......
git 提交代码是会遇到以下问题, git commit 代码时提示: Warning: Your console font probably doesn't support Unicode. If ...
- Linux下制作不用密码可立即登录的SSH用户
一.客户端建立两把钥匙 (1)本例以客户端的monkey用户为例,首先切换到~/.ssh目录下,如果没有该目录的话,需要进行新建 cd ~ mkdir .ssh chmod 700 .ssh cd ~ ...
- pytion3--文档字符串:__doc__
除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看.从语法上来说,这类注释是写成字符串,放在模块文档.函数以及类语句的顶端.就在任何可执行程序代码前(#注释在其前也没问 ...
- Codeforces Round #204 (Div. 2)
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- c++ stack,queue,vector基本操作
stack 的基本操作有:入栈,如例:s.push(x);出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素.访问栈顶,如例:s.top()判断栈空,如例:s.empty(), ...