…and return the number as string.
How can I do that? Is there any library for that?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
std::istringstringfrom<sstream>to convert the strings to integers. Then you can userandorrandomto get a random number, which you can constrain to the interval using modular arithmetic, and then you can convert the number to a hexadecimal string usingstd::ostringstream.#include <sstream> #include <string> #include <cstdlib> int hexstr2int(const std::string& str) { int result; std::istringstream iss(str); iss >> result; return result; } std::string int2hexstr(int number) { std::ostringstream oss; oss << std::hex << number; return number.str(); } int randint(int lower, int upper) { int range = upper - lower; return ((rand()%range)+lower); } std::string randhexstr(const std::string& a, const std::string& b) { int lower = hexstr2int(a); int upper = hexstr2int(b); return int2hexstr(randint(lower,upper)); }