By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

How to break up a number into it's digits and analyze it.

P: n/a
I have a list of numbers i need to analyze, however for this just assume I only need to analyze one. I am given a six to nine digit number which I need to break into pieces from right to left. The right three digits represent an ASCII value while the middle represent a y position and the left three represent the x position, if there are no "left three" the number is assumed to be zero for the x value.

I was thinking that it might be possible to do this by breaking the number into its digits then placing each digit in an array, however I don't know how to do this if it is possible.

Does anyone have suggestions how to do this? Any feedback would be greatly appreciated.
Oct 15 '10 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Yes you can break your integer into an array of character variables.

If you are using C, then do this with a all to itoa().

If you are usign C++, create a stringstream. Then insert your integer into the stream. Then extract from the stream to a char array.
Oct 15 '10 #2

Expert 100+
P: 2,396
Is that six-to-nine-digit number an actual integer number (an unsigned long) or is it a string of decimal-ASCII characters?

You want to break this one number into three pieces: x position, y position, and ASCII value. Each piece consists of three digits. Do you prefer to express the value of each piece as an actual integer number (an int) or as a string of decimal-ASCII characters?
Oct 15 '10 #3

P: 7
Expand|Select|Wrap|Line Numbers
  1. long yournumber = 987654321;
  2. char str[20];
  3. int x,y,asc;
  4.  
  5. sprintf( str, "%ld", yournumber );
  6. sscanf( str, "%3d%3d%3d", &x, &y, &asc );
Oct 16 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.