美文网首页C/C++学习笔记
字符数字和数字之间的相互转换

字符数字和数字之间的相互转换

作者: 零岁的我 | 来源:发表于2020-02-23 14:47 被阅读0次

以字符串形式存储的数字和以数字形式存储的数字之间是有区别的。

例如,字符串 "2679" 就不是一个数字:它是由 2、6、7、9 这 4 个字符的 ASCII 码组成的序列。由于字符串 "2679" 不是一个数字,所以编译器将不允许对它进行加法、乘法和除法之类的数学运算。以数字表示的字符串必须首先转换为数字形式,然后才能与算术运算符一起使用。
当用户在键盘上输入一个数字时,该数字以字符串的形式输入,就好像用户输入的一系列字符(数字)。在 C++ 中,这样的数字通常通过流提取运算符 >> 来读取。在存储到数字类型的变量之前,该运算符会根据需要自动执行转换。在输出期间,数字到字符串的反向转换由流输出运算符 << 执行。


方法总结都放在如下程序注释中了。

#include<iostream>
#include<typeinfo>
#include<sstream>
#include<string>
using namespace std;


int main(int argc,char **argv)
{
//一、数字字符转数字
/*
1)使用字符串对象istringstream:
istringstream 类是从 istream 派生的。它内部包含一个字符串对象,
函数将它作为可以从中"读取"的输入流。
流提取操作符 >> 从封闭的字符串中读取,并在必要时将字符串转换为数字。
*/
    string str1="123";
    cout<<str1<<" "<<typeid(str1).name()<<endl;
    istringstream istr1(str1);

    int i1;
    istr1>>i1;  //将字符数字转换为数字
    cout<<i1<<" "<<typeid(i1).name()<<endl;
/*
2)使用stoX() 系列函数
int stoi(const strings str, size_t* pos = 0, int base = 10)
long stol(const strings str, size_t* pos = 0, int base = 10)
float stof(const strings str, size_t* pos = 0)
double stod(const strings str, size_t* pos = 0)
这些函数可以将 str 可能的最长前缀转换为数字,
并返回一个整数地址pos,pos中保存了str无法被转换的第一个字符的索引。
*/
    int i2=stoi(str1);
    cout<<i2<<" "<<typeid(i2).name()<<endl;
    string str="-342.007 is a double";
    size_t pos;//size_t是在标准库中定义的,常用于表示无符号整数的大小或数组、矢量、字符串中的一个索引
    double d=stod(str,&pos);
    cout<<"The string is:"<<str<<endl;
    cout << "The converted double is " << d << endl;
    cout << "The stopping character is " << str[pos] << " at position " << pos << endl;
/*
3)单个字符转数字,直接用字符减去'0',也就是减去字符0对应的ascii
适用于一位的字符数字转换
*/
    string str2="1";
    cout<<str2<<" "<<typeid(str2).name()<<endl;
    int i3=str2[0]-'0';
    cout<<i3<<" "<<typeid(i3).name()<<endl;

//二、数字转字符
/*
1)使用to_string()函数
*/
    int i=234;
    string str3=to_string(i); //to_string()C++11标准支持
    cout<<str3<<" "<<typeid(str3).name()<<endl;
    long j=345;
    string str4=to_string(i);
    cout<<str4<<" "<<typeid(str4).name()<<endl;
/*
2)一位的数字直接加'0',也就是加0的ascii,可以将该数字直接转换为字符型
*/
    string str5;
    str5=2+'0';
    cout<<str5<<" "<<typeid(str5).name()<<endl;
    return 0;
}

程序运行结果

字符数字转数字值在运用字符串解决大数相加减的问题中尤为重要。


参考链接:http://c.biancheng.net/view/1527.html

欢迎点赞!

相关文章

  • 字符数字和数字之间的相互转换

    以字符串形式存储的数字和以数字形式存储的数字之间是有区别的。 例如,字符串 "2679" 就不是一个数字:它是由 ...

  • 关于JavaScript的隐式转换

    一、 数字与数字之间的转换 1.字符串加数字,数字就会转成字符串。 2.数字减字符串,字符串转成数字。如果字符串不...

  • 算法题常用方法

    一、数据类型相互转换 1.任何 → 字符串 2.任何 → 数字 长得像数字 '数字xxxxx' 'xxxxx数字x...

  • python数据类型转换函数

    参考 python中的字符数字之间的转换函数

  • python2.0+版本--类型转换

    字符串类型的与数字类型的相互转换1.字符串类型的转换为数字类型的str_num = '123'int_num = ...

  • JavaScript == 相等运算符自动转换的4种简单情形

    情景1:比较数字和字符串 比较数字和字符串时,都要将字符串转换为数据,再两个数字进行比较 比如无法转换为数字,是字...

  • 隐式转换

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 数据类型的转换

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 第三天

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

  • 2018-07-11

    隐式转换结论 1、数字 + 字符串 :将数字转换为字符串 2、数字 + boolean :将 boolean 转换...

网友评论

    本文标题:字符数字和数字之间的相互转换

    本文链接:https://www.haomeiwen.com/subject/sqsjqhtx.html