C++ recommends strngstream in STL.

A string is converted to a number

#include<cstdio>
#include<iostream>
#include<algorithm>
#include <string>
#include <sstream>

using namespace std;
int main(a){
    string a="521";// Float and double and so on
    stringstream s;
    int b;
    s<<a;
    s>>b;
    cout<<a<<""<<b*2<<endl;
    return 0;
}


Copy the code

Number to string:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include <string>
#include <sstream>

using namespace std;
int main(a){
    int a = 521;// Float is the same as double etc.;
	string b;
    stringstream s;
    s << a;
    s >> b;/ / write b = s.s tr (); Can also be
	cout<<b<<endl<<a;
    return 0;
}
Copy the code

* In C, atoi, ATOF, ATol, IToa, lTOA are recommended;

The string is converted to numbers atoi,atof,atol

#include<stdio.h>
#include<stdlib.h>

int main(a){
    char *a="521";// This must be an array of characters
    int b=atoi(a); // The same goes for float and double
    printf("%d",b*2);
    return 0;
}

Copy the code

Numbers are converted to strings: IToa, lTOA

#include<stdio.h>
#include<stdlib.h>

int main(a){
    int a=521; / / long in the same way
    char *b;
	itoa(a,b,10);
    printf("%s",b);
    return 0;
}

Copy the code