如何使用C中的Pebble SDK将int转换为字符串

Dav*_*ser 8 c string int pebble-watch pebble-sdk

刚拿到我的Pebble,我正在玩SDK.我是C的新手,但我知道Objective-C.那么有没有办法创建这样的格式化字符串?

int i = 1;
NSString *string = [NSString stringWithFormat:@"%i", i];
Run Code Online (Sandbox Code Playgroud)

我不能用sprintf,因为没有malloc.

我基本上要显示inttext_layer_set_text(&countLayer, i);

The*_*ist 14

用于使用snprintf()整数变量的值填充字符串缓冲区.

/* 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);
Run Code Online (Sandbox Code Playgroud)