stoi causes out of range error
Asked Answered
M

1

10

I have this code which gives me the error terminating with uncaught exception of type std::out_of_range: stoi: out of range

which I have identified as being caused by the line long ascii = std::stoi(temp_string);

what about the way i'm using stoi is causing that and how can I fix it?

std::string encode(std::string message){
std::string num_value;
long cipher;
if(message.length() < 20){
  for(int i = 0; i < message.length(); ++i){
    int temp = (char) message.at(i); 
    num_value += std::to_string(temp); 
  }
  return num_value;
}
else{
    int count = 0;   
    std::string temp_string;
    for(int i = 0; i < message.length(); ++i){
      int temp = (int)  message.at(i);
      temp_string += std::to_string(temp);           
      count++;   
       if(count == 20){
         count = 0;
         //add cipher encrypt
         long ascii = std::stoi(temp_string);
         //cipher = pow(ascii, 10000);
         //add n to cipther encrypt
         //add cipherencrypt + n to num_value
         //reset temp_string
         temp_string += "n";

         temp_string = ""; 

      }
 }
return num_value;
}

int main(){
  std::string s = "Hello World my t's your name aaaaaaaaaaaaa?";
  std::cout<<"encoded value : "<< encode(s)<<std::endl;
}
Manifestation answered 28/4, 2015 at 23:41 Comment(7)
Have you tried printing temp_string to see whether it is in range for an int?Varion
C++ reference has some insight into your exception: "std::out_of_range if the converted value would fall out of the range of the result type or if the underlying function (std::strtol or std::strtoll) sets errno to ERANGE."Marron
What is temp_string = ""; supposed to do?Gallion
Where is your minimal testcase, please? You don't even tell us what your input is!!!Hall
Just appending textual representations of numbers together to make a larger number? You lose the boundaries, now 979 could be either 9,79 or 97,9 (both of which are valid and not terribly unusual ASCII sequences)Norwegian
@LightningRacisinObrit I have added as an edit.Manifestation
You're still missing includes, one } (all of which make your "testcase" fail to compile altogether) and you have compiler warnings. Fix those to produce a minimal testcase, please.Hall
G
18

std::stoi returns an integer; it sounds like your number is too large for an integer. Try using std::stol instead (see here).

Also if you intend for the code to be portable (useable with different compilers/platforms), keep in mind that integers and longs have no standard size. Unless the minimum size given by the standard is enough, you may want to look at using intX_t (where X is the size you want), as given in the cstdint header.

Godber answered 28/4, 2015 at 23:43 Comment(6)
stol does agree better with the variable the result is stored to, however this likely is still not big enough.Norwegian
That's a fair point; I had assumed the long declaration meant the input was constrained, but I don't see any such constraints in the code. I'm not sure what else to recommend, other than moving to arbitrary size libraries.Godber
@BenVoigt the context of the program is an rsa encryption, and my encryption route of choice was to get the ascii values in blocks of 20 chars, and pass them each block into the c= pow(m, e) mod n formula. i'm trying to save the block in a temporary string variable, convert it to a number, put into the formula, and repeat for the amount of blocks.Manifestation
@ralphie9224: At no step in RSA encryption should you be using decimal digits. You can concatenate 20 characters into a large number easily enough using bitshifts, but you'd need a 160 bit data type, which I don't think your C++ compiler has. What datatype is your pow(m, e) mod n function using?Norwegian
I'm using the gmp library to generate sufficiently large numbers, so the values being passed in to that would be past the long or int lengthManifestation
Note to readers: for long long, the function you want is std::stoll.Gel

© 2022 - 2024 — McMap. All rights reserved.