Try this:

int i, k = 0;
for (i = 0; i < n; i++)
    k = 10 * k + a[i];

where n is the length of the array. This is true, however, when the array is short enough otherwise you would get an int overflow.

Answer from sve on Stack Overflow
๐ŸŒ
myCompiler
mycompiler.io โ€บ view โ€บ IGHiCH5
How to convert array of integers into an integer in C (C) - myCompiler
January 5, 2022 - How to convert array of integers into an integer in C (C) - myCompiler
Published: Jan 05, 2022
Discussions

Convert char array to a int number in C - Stack Overflow
So it should be the integer: -1234 using standard libaries in C. I could not find any elegant way to do that. I can append the '\0' for sure. ... Does the possibility exist to append an extra '\0' to the end of the array? More on stackoverflow.com
๐ŸŒ stackoverflow.com
c++ - Converting an Integer Array into a Number - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Save this question. Show activity on this post. ... now I want to convert the whole of this array into an integer number, for example int b become 603215 after running the code. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting an Integer Array to an Intege - C++ Forum
I have an array of integers that I need to convert to a single integer. Is there a function built into C++ that I can use to do so? If so, what is it called and how is it implemented? If not, could someone please walk me through the logic of how that function would work? More on cplusplus.com
๐ŸŒ cplusplus.com
c# - How to convert int array to int? - Stack Overflow
What I would like to learn how to do is to convert an int array to an int in C#. However I want to append the int with the values from the array. Example: int[] array = {5, 6, 2, 4}; Would be More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 6
1

If the size of the array can be bigger you have to understand that a 32 bit integer is good for only nine decimal digits - log10(232), and that even a 64 bit integer is good for 19 decimal digits - log10(264). Either way, even your small example will not fit in a 32 bit int.

Also there is no real mathematical purpose or relationship in appending arbitrary integers in this way, especially as each has a variable number of decimal significant digits - any algorithm would have to take that into account.

So for the reasons given, attempting to solve the problem arithmetically is both over complex and inflexible; the simpler solution is to generate the result as a string.

int A[]={156,23,212,4,12};
int i = 0 ;
int index = 0 ;
const int length_A = sizeof(A) / sizeof(*A) ;
char astr[length_A * 10 ] ; // sized to accommodate worst case 
                            // string length for 32 bit int of INT_MAX.


for( i = 0; i < length_A; i++ )
{
    index += sprintf( &astr[index], "%d", A[i] ) ; 
}

printf( "%s", astr ) ;

If you really need an arithmetic solution and can work within the limits of the available integer data types, then I suggest that you use unsigned long long for the output and unsigned int for the input, then:

#include <stdio.h>
#include <math.h>

int main()
{
    unsigned int A[]={156,23,212,4,12};
    unsigned long long result = 0 ;
    int i = 0 ;
    const int length_A = sizeof(A) / sizeof(*A) ;

    for( i = 0; i < length_A; i++ )
    {
        result += A[i] ;
        if( i < length_A - 1)
        {
            int digits = (int)ceil( log10( (double)A[i+1] ) ) ;
            result *= (int)pow( 10, digits ) ;
        }
    }

    printf( "%llu\n", result ) ;

    return 0 ;
}
2 of 6
0

I see couple of problems:

  1. You are starting the for loop with i=1. It should be i=0.

  2. 15623212412 could be too big to fit in an int. It is on my machine. Use long or a bigger integral type.

By the way, I was able to detect the problem by adding a printf.

#include <stdio.h>

int main()
{
   int A[]={156,23,212,4,12};
   int p, size=5; 
   long n = 0;
   int i;

   for(i=0;i<size;i++){
       p=10;

       while(A[i]>=p)
          p*=10;

       n*=p;  
       n+=A[i];
       printf("n: %ld\n", n);
   }

   return 0;
}
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 21788
Converting an Integer Array to an Intege - C++ Forum
There are several ways to do so: 1. string way: use a stringstream to convert first to a string by adding and so on. 2. math way: use your knowledge about p-adic representation of numbers means: sum(i = 0 to 15) { a_i * p^i } = number and your a_i are your values in the array (a[i]) and p is in your case 10, because it is decimal base.
๐ŸŒ
YouTube
youtube.com โ€บ watch
C - Converting char Array to int - YouTube
C - Converting char Array to int
Published: August 26, 2020
Views: 8K
Find elsewhere
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
Converting an array of digits to an integer - Programming - Arduino Forum
November 9, 2017 - Hello, I'm trying to read using serial communication and use a value as an int, so what I tried to do is place the incoming digits in an array of ints and turn the int array into one single number. I tried to do that and I have realized that the algorithm is very simple but it didn't work using the Arduino. there is what I've tried. int GetNum(int arr[]) { int sum = 0; for (int i = 0; i
๐ŸŒ
GitHub
gist.github.com โ€บ shaunlgs โ€บ 11196f2320e9013c000320f6e538a98b
Convert char array into int array in C. ยท GitHub
Save shaunlgs/11196f2320e9013c000320f6e538a98b to your computer and use it in GitHub Desktop. ... Convert char array into int array in C.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ how-to-convert-char-array-to-int-in-cpp
How to Convert Char Array to Int in C++ - GeeksforGeeks
July 23, 2025 - // C++ Program to Convert Char Array to Int #include <iostream> #include <cstdlib> using namespace std; int main() { char str[] = "12345"; int num = atoi(str); cout << "The integer value is: " << num << endl; return 0; }
๐ŸŒ
Processing Forum
forum.processing.org โ€บ topic โ€บ how-do-i-convert-an-int-array-to-an-integer
How do i convert an int array to an integer? - Processing Forum
For example I have an array of integers: int[] array =new int[7]; I then put integers in few slots of the array and now I want all those slots to be concatenated into one integer so for example if my array was: array[0] = 1 array[1] = 3 array[2] = 5 array[3] = 6 i want a new integer lets say ...
๐ŸŒ
Reddit
reddit.com โ€บ r/askcomputerscience โ€บ how do i convert a byte array into an integer in c?
r/AskComputerScience on Reddit: How do I convert a byte array into an integer in c?
September 21, 2022 -

I have a byte array that looks like [E1,DD,5,2] coming from a CAN bus.

The hex string should be something like: "0205DDE1" which equals a value of 33938917.

I have no idea what I'm doing wrong, my code so far looks like:

int32_t val = ((arr[3] << 32) | (arr[2] << 16) | (arr[1] << 8) | arr[0]);

๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 743639 โ€บ languages โ€บ convert-char-array-int-array
How to convert char array to int array in C (C / C++ forum at Coderanch)
June 27, 2021 - John Matthews wrote: Can you explain what line 3 does in your code? Converts a digit character into a digit value; And does the compiler know that it's a "int" when doing the operations? ... In short, yes; lookup 'integer promotion' eg.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ general โ€บ 117681
Convert Int array to an integer - C++ Forum
may want to use stringstreams. http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream ... Remember your basic math. This is kind of the opposite of logarithm -- each element is multiplied by a power of ten. 123 = 1ร—102 + 2ร—101 + 3ร—100 Another example: 7926 = 7ร—103 + 9ร—102 + 2ร—101 + 6ร—100 The simplest way for your array is an iterative approach: Hope this helps. Topic archived.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 170279-convert-character-array-integer-array-c.html
convert character array to integer array in c
April 11, 2016 - '0' is 48 so 48 - 48 is 0. '1' is 49 so 49 - 48 is 1. You can also use atoi the function to convert each character to an integer but the problem with this is that it does not protect you against overflow and underflow.(Now that I recall I think atoi uses the ASCII approach as well) My personal recommendation is to strtol as it tells you if you got an overflow or underflow and helps you when you are doing validation.
๐ŸŒ
Julia Programming Language
discourse.julialang.org โ€บ new to julia
Convert an array into Int - New to Julia - Julia Programming Language
July 14, 2019 - Hi, If I have an 2D array of multiple elements (named aux), how do I convert or ensure that they are integers? Also note that I didnโ€™t create this array, it was copied from a dataframe, which was in turn taken from a Cโ€ฆ