read individual digits from a number without % or /

I have a large number that cannot be stored in a long long (or anything smaller, obviously). I want to read the individual digits from this number. I am not quite sure how to do this. I cannot use the / or % method because the number cannot fit in a variable. My current thought process is to:

1. store the large number in a string.
2. take the string one letter at a time, convert the number in the string to an int.
3. put the integer in a vector with push_back();


But I am unsure how to read the string one character at a time. Will I have to use the c-style character array? Or is there a way to do this with strings.

Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <vector>

int main()
{
    // store the large number in a string.
    std::string number = "12345678901234567890123456789012345678901234567890" ;

    std::vector<int> digits ;

    // take the string one letter at a time
    for( char c : number )
    {
        // convert the number in the string to an int.
        const int d = c - '0' ;

        // put the integer in a vector with push_back();
        digits.push_back(d) ;
    }
}
Google around "bignum" for more.
Also, http://www.cplusplus.com/forum/lounge/32041/
I see this exact same question all the time and never take note of the favorite answers.
Anyways in my opinion if the number is too large for a 64bit integer type then I'd suggest searching for a library (or creating your own) that defines numbers in a different way. Creating a class that stores its numbers as a string, the only problem being that you'd need to also redefine how basic mathematics would work for these types.
I'd probably store the string keeping the number as a binary representation (just a really long one), and do something really complex to do with binary mathematics and working with a vector of strings and recursive functions.

Maybe take a look at binary mathematics, if not for this then just for fun because it's so easy... And interesting XD
my final solution was to read it into a string and take it apart character by character, using a character to int conversion. thanks for your help, everyone!
Topic archived. No new replies allowed.