Jump to content
  • Advertisement

Archived

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

executor_2k2

Parsing an integer for certain digits

This topic is 5284 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!