0

Is there an easy way to do the following:

Convert a byte array like so {1,3,0,2,4} to a char array like so {'1','3','0','2','4'} or "13024".

I can do the following ( I think ) but it is more cumbersome:

            itoa(byte_arr[0],cap_periph[0],10);
            itoa(byte_arr[1],cap_periph[1],10);
            itoa(byte_arr[2],cap_periph[2],10);

Something that works on avr-gcc as well.

Garf365
  • 3,619
  • 5
  • 29
  • 41

1 Answers1

1

The main point is to use a loop, whatever implementation you use. If you are totally sure that each element inside source array is between 0 and 9:

// Only works if each element of byte_arr is between 0 and 9
for(int i = 0; i < 3; ++i)
{
    cap_periph[i] = byte_arr[i] + '0';
}
cap_periph[3]  = '\0';
Garf365
  • 3,619
  • 5
  • 29
  • 41
  • `itoa` is not a standard function, and the second argument is not right. Also there may be issues for both cases if the elements are not between 0-9. (buffer overflows in particular in the first case). – M.M Dec 20 '16 at 08:41
  • Right, I only leave second solution, with warning, because OP's case seams to have only number between 0 and 9 – Garf365 Dec 20 '16 at 08:44
  • That is indeed the case. Thanks! –  Dec 20 '16 at 08:46