+ 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

17th Dec 2016, 10:45 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
11 ответов
+ 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; }
25th Jan 2017, 4:39 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
But i dont know why is it automatically converting binary to decimal :)
25th Jan 2017, 4:52 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
But i dont know why is it automatically converting binary to decimal :)
25th Jan 2017, 4:53 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
only for nos as large as this...
25th Jan 2017, 4:54 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
your welcome ! if you had problem consider me as your firiend ! :)
17th Dec 2016, 1:59 PM
Amir Goodarzi
Amir Goodarzi - avatar
+ 1
bourhan ! your code is wrong ! you cant convert string to int directly ...
17th Dec 2016, 10:52 AM
Amir Goodarzi
Amir Goodarzi - avatar
17th Dec 2016, 1:43 PM
Amir Goodarzi
Amir Goodarzi - avatar
+ 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; }
17th Dec 2016, 1:43 PM
Amir Goodarzi
Amir Goodarzi - avatar
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
17th Dec 2016, 11:05 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
0
@Bourhan that doesn't work properly, sorry :(
17th Dec 2016, 11:10 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
0
I think I got it, thank you @Amir
17th Dec 2016, 1:53 PM
Catalin Dervesteanu
Catalin Dervesteanu - avatar