Help on concatening array elements

Hey there!

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.

I very much appreciate the help.

(byte)((10 * (x[2] - '0')) + (x[3] - '0'))

Wow, that worked. And it is just that simple. Thank you!

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.

hadriel26:
Wow, that worked. And it is just that simple.

Yup. char is essentially "tiny int" so anything you can do with int you can do with char.

Thank you!

You are welcome.