Archived

This topic is now archived and is closed to further replies.

executor_2k2

Parsing an integer for certain digits

Recommended Posts

Im writing in C++ and I was wodering if there are any functions in the standard library or an Integer class that would allow me to pull out certain digits of an integer. Ex pull out the 5 of 456. Thanks.

Share this post


Link to post
Share on other sites
If you want the n''th digit, divide by 1e(n-1), mod by 10 and the last result should be the digit you want. I think thats roughly how to do it.

Share this post


Link to post
Share on other sites
itoa is a common extention, you could also use sprintf to convert it to a string. (Then you can read the digits and substract ''0'' to get the value).

char digits[33];
int x = 43713454;
size_t n = strlen(itoa(x, digits, 10));

int third = digits[n-3] - ''0'';

Share this post


Link to post
Share on other sites