Thread: Converting multiple individual array members into one number?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    15

    Converting multiple individual array members into one number?

    I have an array,members 5-7 are numbers 1, 0, and 5. (or any other three numbers of your choosing). These three numbers need to be changed into a single number 105 and stored into a variable. The beginning of the array may contain text. I know exactly where the first number will start in the array, but it may be 1 or 1000, what ever it is I need to convert it to a single number for a variable.

    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What is your question? Can you provide the code you have so far? Be sure to use code tags.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    My question is "Is there a way to convert members of an array into a single number?"
    for example,

    buffer[7] = ['S', 'T', 0, 0, 1, 0, 5]//Need code to take 1, 0, 5 out of array as a single number, being 105.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    The code is not straight forward. I have individual characters being sent to my micro via serial (virtual com port set up) com. Each character send is stored into a buffer. If the correct sequence if characters is sent, I tell the program to do x,y,z. Sometimes I need to take a series of numbers our of the array as a single number to be stored into flash. I do not know of an "elegant" way to do this.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Then you need to develop an algorithm to convert individual digits into a single number. This shouldn't be too difficult, if you're somewhat familiar with:

    - declaring variables
    - arrays
    - basic loops
    - multiplication

    If you're stuck on where to start, take out some paper and a pen and do it by hand first, step by step. Figure out how you would find where to start looking for numbers in an array, and how to turn a small list of individual numbers into a complete number as required. Keep track of your steps, and this will give you a general algorithm to follow. Only then should you start actual coding.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    ....thanks lol. I could easily do that but I want to know if I am missing a function or something that could take care of this.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Oh! I see

    I personally don't know of any functions (standard or otherwise) that will perform this particular task. Sorry I couldn't have been of more help.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to meet Mr. OK and Mr. Num. But first, welcome to the forum, DanielB33!

    Unless you know the function exists, it's difficult to put together an algorithm.

    The header file ctype.h, has functions that will give tests for different types of data. This is an example of using one named isdigit().

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void) {
       int i,j,num,len;
       char data[20]={"abc10def20ghi999"};
       len=strlen(data);
       char *ok=malloc(len);
    
       if(!ok) {
          printf("memory allocation failed!\n");
          return 1;
       }
       
       for(i=0,j=0;i<len;i++) { //scan the array
          if(isdigit(data[i])) {    //if the char is a digit
             ok[j]=data[i];        //copy it over to ok[]
             ++j;                   //to the next free index location
          }
       }
       
       printf("%s\n",ok);    //number as a string
       num=atoi(ok);        //convert it to an int
       printf("%d\n",num); //and show it
       free(ok);              //free the malloc'd array memory
       return 0;
    }
    This is a freebie, but follow the advice given by Matticus above:

    Always surround any code you post with code tags
    Post an attempt to solve the problem. Otherwise, the forum tends to become a "do my homework for me" site, for hordes of students.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Would it work to do a loop that continually adds itself up by 1 until it reaches the first & largest digit of the array, and then adds itself up until it reaches the 2nd digit of the array, and then finally the third digit of the array?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It would - sticky wicket when you reach the first multi-digit numbers, though.

    You could build up the number using the base 10 and multiplication:
    digit * 10^0, digit * 10^1, digit * 10^2,

    etc., but then you probably should move backward through the string to build it up, and it gets more involved than necessary.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    There would be no need to move backward through the string. Assuming all the digits to be extracted are in continuous elements (implied by the OP: "...take a series of numbers out of the array"), and that there's only one number in the string, this can easily be accomplished.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        int i;
        int total = 0;
        char array[] = "ABC01645ff";
    
        for(i=0; array[i] != '\0'; i++)
        {
            /* checks if the element is a digit */
            if(isdigit(array[i]))
            {
                /* ignores leading zeroes */
                if(array[i] == '0' && total == 0)
                    continue;
                else
                {
                    total = total * 10;
                    total = total + (array[i] - '0');
                }
            }
        }
    
        printf("Original string:  %s\n",array);
        printf("Extracted value:  %d\n",total);
    
        return 0;
    }

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by DanielB33 View Post
    buffer[7] = ['S', 'T', 0, 0, 1, 0, 5]//Need code to take 1, 0, 5 out of array as a single number, being 105.
    Basic arithmetic will work:
    Code:
     (buffer[4]*10 + buffer[5])*10 + buffer[6]
    Or, if you want that as a function,
    Code:
    unsigned int base10(const char *const buffer, const int len)
    {
        unsigned int  value = 0U;
        int  i;
    
        for (i = 0; i < len; i++)
            value = 10U * value + buffer[i];
    
        return value;
    }
    in which case you'd use base10(buffer + 4, 3) to get the actual value of the three digits starting at the fifth element ([0] is first, [1] is second, so [4] is fifth) in buffer.

    This is very similar to what Adak showed above. The only difference is that Adak assumes you had actual characters, decimal digits '1', '0', '3' in the buffer, whereas this one assumes they are the actual values 1, 0, and 3 (i.e. ASCII characters SOH, NUL '\0', and EOT).

    (In other words, the only real difference is that if the buffer has ASCII characters, then you use (buffer[index] - '0'), but if they are actual decimal digit values and not characters, then you use buffer[index]).

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by DanielB33 View Post
    My question is "Is there a way to convert members of an array into a single number?"
    for example,

    buffer[7] = ['S', 'T', 0, 0, 1, 0, 5]//Need code to take 1, 0, 5 out of array as a single number, being 105.
    How about just using sscanf?
    Code:
    int x;
    char numbuf[6] = "";
    memcpy(numbuf, &buffer[2], 5);
    for (int i=0; i < 6; i++)
       numbuf[i] += '0';
    sscanf(numbuf, "%d", &x);

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The simplest answer is to use sprintf and print the numbers like this: sprintf(printbuf,"%d%d%d",num1,num2,num3);

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    15
    Thank you all for adding to the discussion, sorry for my delayed response. I did not expect such great answers! I will use this forum more.

    I ended up using atoi() function, sending a pointer to the array place I new the numbers would be at, works like a charm. I ams till having some problems with the code thought and can't find the bug. See my latest post to contribute!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to access individual characters in an array?
    By LanguidLegend in forum C Programming
    Replies: 2
    Last Post: 02-08-2011, 06:29 AM
  2. Manipulating individual bits in a double precision number
    By thetinman in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2010, 07:19 AM
  3. Storing an int as individual digits in an array
    By Wiretron in forum C Programming
    Replies: 11
    Last Post: 12-17-2007, 10:21 AM
  4. Reading individual strings from an array.
    By Baaaah! in forum C Programming
    Replies: 2
    Last Post: 12-20-2005, 03:42 PM
  5. Converting a struct members to a string, c newbie!
    By djwiltsh in forum C Programming
    Replies: 1
    Last Post: 08-14-2003, 05:33 AM