c++ - stoi causes out of range error -
i have code gives me error terminating uncaught exception of type std::out_of_range: stoi: out of range
which have identified being caused line long ascii = std::stoi(temp_string);
what way i'm using stoi causing , how can fix it?
std::string encode(std::string message){ std::string num_value; long cipher; if(message.length() < 20){ for(int = 0; < 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 = 0; < 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 cipther encrypt //add cipherencrypt + n num_value //reset temp_string temp_string += "n"; temp_string = ""; } } return num_value; } int main(){ std::string s = "hello world t's name aaaaaaaaaaaaa?"; std::cout<<"encoded value : "<< encode(s)<<std::endl; }
std::stoi returns integer; sounds number large integer. try using std::stol instead (see here).
also if intend code portable (useable different compilers/platforms), keep in mind integers , longs have no standard size. unless minimum size given standard enough, may want @ using intx_t (where x size want), given in cstdint header.
Comments
Post a Comment