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) ...
随机推荐
- 条款39:明智而审慎地使用private继承(use private inheritance judiciously)
NOTE: 1.private 继承意味 is-implemented-in-terms-of(根据某物实现出).它通常比复合(composition)的级别低.但是当derivated class需 ...
- Linux三剑客之sed详解(2)
一.sed 分组替换(),\1 实例:I am a oldboy teacher. 吧oldboy 提取出来 二.特殊符号&代表被替换的字符串 实例:批量替换文件名 把stu_102999_1 ...
- Web框架之Django_03 路由层了解(路有层 无名分组、有名分组、反向解析、路由分发 视图层 JsonResponse,FBV、CBV、文件上传)
摘要: 路由层 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态网页.虚拟环境 视图层 JsonResponse FBV 与 CBV(function base views与class bas ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- C#自定义Excel操作类
C#自定义Excel操作类,可以用于将DataTable导出到Excel文件,从Excel文件读取数据. using System; using System.IO; using System.Dat ...
- 脑阔疼的双层SQLserver游标
本来简单的双层游标没啥的,内层游标需要读取的是视图的内容,一直报“当前命令发生了严重错误.应放弃任何可能产生的结果.”的错误.无可奈何尝试先将视图的数据放到表变量中,之后再用游标遍历表变量. 简直很怀 ...
- oracle create directory
1.新建directory的语法 CREATE [OR REPLACE] DIRECTORY directory AS 'pathname'; 例如: create or replace direct ...
- iOS学习笔记16-数据库SQLite
一.数据库 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等.离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式: 归档:NSKeyedArchiver 偏好设置:NSU ...
- 蛋疼的SVG外部引用方式
SVG在html页面中有两种引用方式: 1. 内联.就是直接将SVG图形写在html的svg标签中,比如: <html> <head></head> <bod ...
- POJ——2251Dungeon Master(三维BFS)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25379 Accepted: 9856 D ...