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;
}
Rafa
  • 3,219
  • 4
  • 38
  • 70
  • Have you tried printing `temp_string` to see whether it is in range for an `int`? – merlin2011 Apr 28 '15 at 23:43
  • 1
    [C++ reference](http://en.cppreference.com/w/cpp/string/basic_string/stol) 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." – adamdc78 Apr 28 '15 at 23:43
  • What is `temp_string = "";` supposed to do? – πάντα ῥεῖ Apr 28 '15 at 23:43
  • 2
    Where is your minimal testcase, please? You don't even tell us what your input is!!! – Lightness Races in Orbit Apr 28 '15 at 23:46
  • 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) – Ben Voigt Apr 28 '15 at 23:50
  • @LightningRacisinObrit I have added as an edit. – Rafa Apr 28 '15 at 23:50
  • 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. – Lightness Races in Orbit Apr 28 '15 at 23:53

1 Answers1

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.

sabreitweiser
  • 643
  • 3
  • 13
  • 1
    `stol` does agree better with the variable the result is stored to, however this likely is still not big enough. – Ben Voigt Apr 28 '15 at 23:48
  • 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. – sabreitweiser Apr 28 '15 at 23:55
  • @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. – Rafa Apr 28 '15 at 23:57
  • 1
    @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? – Ben Voigt Apr 29 '15 at 00:01
  • 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 length – Rafa Apr 29 '15 at 00:26
  • Note to readers: for `long long`, the function you want is `std::stoll`. – Caleb Stanford Dec 12 '20 at 23:34