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.
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.
Here is a function I made
int array_to_num(int arr[],int n){
char str[6][3];
int i;
char number[13] = {'\n'};
for(i=0;i<n;i++) sprintf(str[i],"%d",arr[i]);
for(i=0;i<n;i++)strcat(number,str[i]);
i = atoi(number);
return i;
}
where str[6][3] means there are 6 elements that can hold 2 digit numbers, change it to suit your needs better. Also n is the size of the array you put into the function.
Use it like this:
int num[6] = {13,20,6,4,3,55};
int real_num;
real_num = array_to_num(num,6);
real_num will now be 132064355
Convert char array to a int number in C - Stack Overflow
c++ - Converting an Integer Array into a Number - Stack Overflow
Converting an Integer Array to an Intege - C++ Forum
c# - How to convert int array to int? - Stack Overflow
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 ;
}
I see couple of problems:
You are starting the
forloop withi=1. It should bei=0.15623212412could be too big to fit in anint. It is on my machine. Uselongor 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;
}
I personally don't like atoi function. I would suggest sscanf:
char myarray[5] = {'-', '1', '2', '3', '\0'};
int i;
sscanf(myarray, "%d", &i);
It's very standard, it's in the stdio.h library :)
And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end.
EDIT
I just found this wonderful question here on the site that explains and compares 3 different ways to do it - atoi, sscanf and strtol. Also, there is a nice more-detailed insight into sscanf (actually, the whole family of *scanf functions).
EDIT2
Looks like it's not just me personally disliking the atoi function. Here's a link to an answer explaining that the atoi function is deprecated and should not be used in newer code.
Why not just use atoi? For example:
char myarray[4] = {'-','1','2','3'};
int i = atoi(myarray);
printf("%d\n", i);
Gives me, as expected:
-123
Update: why not - the character array is not null terminated. Doh!
Use a std::stringstream:
#include <iostream>
#include <sstream>
int main() {
std::stringstream ss;
int arr[] = {60, 321, 5};
for (unsigned i = 0; i < sizeof arr / sizeof arr [0]; ++i)
ss << arr [i];
int result;
ss >> result;
std::cout << result; //603215
}
Note that in C++11 that mildly ugly loop can be replaced with this:
for (int i : arr)
ss << i;
Also, seeing as how there is a good possibility of overflow, the string form of the number can be accessed with ss.str(). To get around overflow, it might be easier working with that than trying to cram it into an integer. Negative values should be taken into consideration, too, as this will only work (and make sense) if the first value is negative.
int a[] = {60, 321, 5};
int finalNumber = 0;
for (int i = 0; i < a.length; i++) {
int num = a[i];
if (num != 0) {
while (num > 0) {
finalNumber *= 10;
num /= 10;
}
finalNumber += a[i];
} else {
finalNumber *= 10;
}
}
finalNumber has a result: 603215
simply multiply each number with 10^ his place in the array.
int[] array = { 5, 6, 2, 4 };
int finalScore = 0;
for (int i = 0; i < array.Length; i++)
{
finalScore += array[i] * Convert.ToInt32(Math.Pow(10, array.Length-i-1));
}
int output = array
.Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
.Sum();
You basiclly have the answer but you would be better doing it from the other direction ie:
int multiplier = 1;
for (i = 7; i >= 0; --i )
{
bin += (multiplier * arrbin[i]);
multiplier *= 10;
}
that way you can change the length of the array you are using easily.
How about do it manually, the same way you do it to convert binary to int using powers of 2
#include <stdio.h>
#include <string.h>
main()
{
int arr[4]={1,0,0,1};
int i;
int output=0, power=1;
for (i=0; i<4; i++)
{
output += arr[3-i]*power;
power *= 2;
}
printf("%d\n", output);
in this example
arr = 1 0 0 1
output = 1*2^0 + 0*2^1 + 0*2^2 + 1*2^3 = 9
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]);