0

I need to convert an integer array of size 4 into an int. I've seen solutions for int arrays that look like {1, 2, 3, 4} turn into 1234, and I've also seen ones where an array like {10, 20, 30} would turn into 102030. However, my problem is that I'll have an array that might look like {0, 6, 88, 54} and the solutions I previously mentioned only work on arrays with ints of the same type {e.g all one digit or all two digit}.

What should I do to solve this?

My expected output from the {0, 6, 88, 54} array would be 68854.


Examples An output with zeros in the middle should keep them, i.e. {6, 0, 0, 8} would be 6008 but {0, 6, 0, 0, 8} by default would still be 6008 in int form. I need this in an int but I wouldn't mind having a string intermediate.

5
  • 2
    For {0, 6, 88, 54} what is your expected result? Commented Mar 24, 2018 at 0:21
  • 1
    I would use itoa on each element of the array and then use strcat to append each string to the output string. Commented Mar 24, 2018 at 0:26
  • Is the output a string or a number? Commented Mar 24, 2018 at 0:27
  • Right. So what would the expected output be for [6, 0, 0, 8]? Commented Mar 24, 2018 at 0:27
  • An output with zeros in the middle should keep them, ie {6, 0, 0, 8} would be 6008 but {0, 6, 0, 0, 8} by default would still be 6008 in int form. I need this in an int but I wouldn't mind having a string intermediate Commented Mar 24, 2018 at 0:35

3 Answers 3

2

You could do something like this:

int res = 0;
int nums[4] = {1, 4, 3, 2}

int i = 0, temp;
for (i = 0; i < 4; ++i) {
  temp = nums[i];
  do {
    res *= 10;
  } while ((temp /= 10) > 0);


  res += nums[i];
}
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work on mixed digit numbers, only single digit numbers
@bmw417 for me it's not clear what number you expect from {0, 6, 88, 54}
It would be 68854
@bmw417 so I have fixed the answer and it works even with this weird example Pablo gave as an example.
1

What about this solution?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int arr[] = {0, 6, 88, 54};

    char buffer[1000] = { 0 };

    for(size_t i = 0; i < sizeof arr / sizeof *arr; ++i)
        sprintf(buffer, "%s%d", buffer, arr[i]);

    int val = strtol(buffer, NULL, 10);
    printf("%d\n", val);

    return 0;
}

int prints 608854.

Comments

1

a neat solution would perhaps be to print to a string then convert the string back to an integer

char dummy[100];
int answer;
int input[4];

....

sprintf(dummy,"%d%d%d%d",input[0],input[1],input[2],input[3]);
answer=atoi(dummy);

the sprintf prints your integers into a string

the atoi converts the string into your integer, and it should be able to handle a 0 at the front.

full program

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char dummy[100];
  int answer;
  int input[4]={3,4,0,345};

  sprintf(dummy,"%d%d%d%d",input[0],input[1],input[2],input[3]);
  answer=atoi(dummy);

  printf("%d\n",answer);

  return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.