Getting part of a number
I have been wondering about this for a while, is it possible to extract a single digit from a longer number? Say you have the number 123456, is it possible to just get the second from the front? The only way I can come up with it is to convert the number into a string.
Divide by 10000 and take modulo 10?
Of course, that assumes the number is 6 digits long. But the same principle holds for any size integer that fits in one of the integer types that your compiler provides.
That's actually not such a bad way of doing it, since sprintf() is going to use the divide-and-modulo method to do the conversion, which is the same thing you would have done anyway. Provided it's easy to create the buffer for the string conversion, I think converting to string and then accessing the digits is not a bad method.
Originally Posted by h3ro