itoa function for byte array

781 views Asked by At

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.

1

There are 1 answers

3
Garf365 On BEST ANSWER

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';