+ 1
How to convert a character form a string to an integer?
I need this to convert a number from binary to decimal, the number will be larger than 100 digits
11 Réponses
+ 5
Save the number in a string, and use this function like this (from <sstream>):
#include<iostream>
#include<sstream>
using namespace std;
int main(void)
{
long int value;
char str[1000]="1010110101010110110111011";
stringstream(str)>>value;
cout<<value<<endl;
}
+ 3
But i dont know why is it automatically converting binary to decimal :)
+ 3
But i dont know why is it automatically converting binary to decimal :)
+ 3
only for nos as large as this...
+ 2
your welcome ! if you had problem consider me as your firiend ! :)
+ 1
bourhan ! your code is wrong ! you cant convert string to int directly ...
+ 1
Catalin read this code :
http://www.sololearn.com/app/cplusplus/playground/cHE1eL0C6sWG/
+ 1
#include <iostream>
#include<string>
using namespace std;
int main() {
// a string variable :
string s;
cin >> s;
// an array of integers for storing numbers :
int numbers[1000];
int j = 0;
for (int i = 0;i<s.length();i++)
{
if ((int)s[i] >= 48 && (int)s[i] <= 57)
{
j++;
numbers[j] = ((int)s[i] - 48);
}
}
//output numbers :
if (j != 0)
for (int i = 1;i <= j;i++)
cout << numbers[i] << endl;
return 0;
}
0
@Amir Goodarzi I don't want to convert a string to int, I want to convert a char from inside a string to int, and this can be done from what I heard
0
@Bourhan that doesn't work properly, sorry :(
0
I think I got it, thank you @Amir