How to convert int to string with Pebble SDK in C

c, int, pebble-sdk, pebble-watch, string

Solution

Use `snprintf()` to fill a string buffer with the value of the integer variable.

/* the integer to convert to string */
static int i = 42;

/* The string/char-buffer to hold the string representation of int.
 * Assuming a 4byte int, this needs to be a maximum of upto 12bytes.
 * to hold the number, optional negative sign and the NUL-terminator.
 */
static char buf[] = "00000000000";    /* <-- implicit NUL-terminator at the end here */

snprintf(buf, sizeof(buf), "%d", i);

/* buf now contains the string representation of int i
 * i.e. {'4', '2', 'NUL', ... }
 */
text_layer_set_text(&countLayer, buf);

Problem

Just got my Pebble, and I am playing around with the SDK. I am new to C, but I know Objective-C. So is there a way to create a formatted string like this? ``` int i = 1; NSString *string = [NSString stringWithFormat:@"%i", i]; ``` And I can't use `sprintf`, because there is NO `malloc`. I basically want to display an `int` with `text_layer_set_text(&countLayer, i);`

Original source

Related problems