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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan LaBrecque

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

    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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?

      Comment

      • BillyTKid
        New Member
        • Aug 2010
        • 7

        #4
        Code:
        long yournumber = 987654321;
        char str[20];
        int x,y,asc;
        
        sprintf( str, "%ld", yournumber );
        sscanf( str, "%3d%3d%3d", &x, &y, &asc );

        Comment

        Working...