Search code examples
cintegerstring-conversion

Convert multiple Integers into a single string


I have a function that pulls the X,Y,Z co-ords of an accelerometer and stores them in 3 individual integers.

I want to send these via a wireless chip on my microcontroller, but the transmit function takes a string parameter.

So I want to combine the integers into one string and then send this string.

I have:

    Int xData;
    Int yData;
    Int zData;

char totalData[64]

But would like to combine them into a string that is something like this:

("X:" + xData + " Y:" + yData + " Z: "xData)

Solution

  • Use sprintf. It works like printf, but the output is put into a char array instead of being sento to stdout:

    sprintf(totalData, "X:%d: Y:%d: Z:%d", xData, yData, zData);