Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a 340 digit number(actually many numbers of similar length):
1129711511511911111410096234183176109119206163129208102132631072477671382311641228010717710121397160216130941034422491351531952542142042212010417692502361865016015813621817721819614024011173210721117018713290631941066622184168211118201220201551611586422931177241021224113946112515922518478624313439126015210223973231982481351582251272696193
I need some way to convert it to an int(it's a string right now). I am able to have a variable that big in Python, but now I'm working in c++.

What I have tried:

I need it as an int so I can run it through a function I have:
string decimalToBinary(vector<int> decimal, int length = 7)
{
    string binary;

    for (int dec: decimal)
    {
        for (int i = length; i >= 0; i--) 
        { 
            int k = dec >> i; 
            if (k & 1) 
            {
                binary.append("1");
            } 
            else
            {
                binary.append("0");
            }
        } 
    }
    return binary;
}

I've tried converting it with std::stoll, but I still get the 'out_or_range' error. Is there any way to do this, or do it without converting to int?
Posted
Updated 3-Mar-21 19:16pm
v2
Comments
Peter_in_2780 4-Mar-21 1:21am    
The largest integer type commonly supported is "long long", usually 64 or 128 bits. Nowhere near big enough for you!
Search for "c++ bigint" to find arbitrary precision packages.
Captian Ahab 4-Mar-21 11:57am    
So I found one I like the look of herehttps://github.com/sercantutar/infint, but how will I include it once I download?

1 solution

Quote:
What is the size limit for a C++ number?

Here is alittle reference standard datatypes in c++ :
C data types - Wikipedia[^]
C - Data Types - Tutorialspoint[^]
Beyond this, it is BigIntegers or multiple precision :
GNU Multiple Precision Arithmetic Library - Wikipedia[^]
List of C++ multiple precision arithmetic libraries - Wikipedia[^]
Arbitrary-precision arithmetic - Wikipedia[^]
 
Share this answer
 
v2
Comments
CPallini 4-Mar-21 2:10am    
5.
Patrice T 4-Mar-21 2:30am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900