...and return the number as string.
How can I do that? Is there any library for that?
...and return the number as string.
How can I do that? Is there any library for that?
boost::lexical_cast is pretty useful and convenient for converting between strings and numbers. It lets you write expressions like:
int some_number = boost::lexical_cast<int>(some_string);
or the other way:
std::string some_string = boost::lexical_cast<std::string>(some_int);
My reply isn't going to be as useful as others, but this is really a basic programming problem.
Learning to program consists in some way in learning how to break problems into subproblems, up to a point where the small problem is manageable.
You want to randomize a number between two hex numbers, represented as strings.
The starting point here is that you want a random number. Finding out how to get a random number shouldn't be that complicated.
Now perhaps the random number function you have doesn't accept a range.
Say for example it produces floating numbers between 0 and 1, you want numbers between a and b. It's not hard to find you can convert a number n from 0 - 1 to a number m from a - b : m = n*(b-a)+a
Sometimes you'll have a random function that gives an integer number between 0 and the given argument; sometimes it'll be different -- apply the same reasoning.
Then you need to find the a and b values for your range, but you have strings representing hex numbers. Well you'll obviously will have to find a way to convert them to decimal numbers. It shouldn't be hard to find a function that does that for you. If you don't, parsing a hex string isn't that hard.
And voilà. By breaking the problem into subproblems, you managed to find your way to the solution.