I have an array as such: "char x[10]". I need to concatenate x[2] with x[3] (not suming them up), but 'joining' them. For example: x[2] = 8 and x[3] = 6. I need to store in another variable the value of "86", preferrably converted to the integer type.
How could I proceed? I tried using the atoi() function, but I always get errors.
If you know how many digits (say three) you can do this:
take first digit, multiply by 10^3 + second * 10^2 + third * 10 + fourth
If you don’t know how many it’s a little trickier to understand:
const int sz = 4;
int ary[sz]= {6, 3, 7, 2 }; // want 6372 out of this
int ret; // variable to store output
for (int i=0; i<sz; i++) {
ret = ret * 10; // Shift the numbers one digit over
ret = ret + ary[i];
}
if the array is a bunch of digits encoded as ASCII, simply subtract ‘0’ (notice the single quotes) from them before using.