# Converting multiple individual array members into one number?

• 12-07-2012
DanielB33
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!
• 12-07-2012
Matticus
What is your question? Can you provide the code you have so far? Be sure to use code tags.
• 12-07-2012
DanielB33
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.
• 12-07-2012
DanielB33
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.
• 12-07-2012
Matticus
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.
• 12-07-2012
DanielB33
....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.
• 12-07-2012
Matticus
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.
• 12-07-2012
You need to meet Mr. OK and Mr. Num. But first, welcome to the forum, DanielB33! :cool:

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.
• 12-07-2012
gratiafide
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?
• 12-07-2012
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.
• 12-07-2012
Matticus
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-08-2012
Nominal Animal
Quote:

Originally Posted by DanielB33
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]).
• 12-08-2012
c99tutorial
Quote:

Originally Posted by DanielB33
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.

```int x; char numbuf[6] = ""; memcpy(numbuf, &buffer[2], 5); for (int i=0; i < 6; i++)   numbuf[i] += '0'; sscanf(numbuf, "%d", &x);```