How to convert int to string with Pebble SDK in C
Asked Answered
P

2

8

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

Pelasgian answered 6/12, 2013 at 23:38 Comment(5)
"And I cant use sprintf, cause there is NO malloc." - WAT? char buf[0x100]; snprintf(buf, sizeof buf, "%d", i);...Forebode
Also, this is not a cast by any means.Forebode
@H2CO3 Thanks, I tweaked your answer a bit, so it works for me.Pelasgian
You should post the solution as an answer (not an edit) and accept it. It will greatly help future visitors.Capybara
Moved the solution "update" to a proper answer.Geisler
G
14

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);
Geisler answered 3/4, 2014 at 16:8 Comment(1)
Was this on an older SDK ? It creates a "App fault!" error for me :(Sarabia
L
3

My favorite 2 generic integer to string functions are as follows. The first will convert a base 10 integer to a string, the second works for any base (e.g. binary (base 2), hex (16), oct (8) or decimal (10)):

/* simple base 10 only itoa */
char *
itoa10 (int value, char *result)
{
    char const digit[] = "0123456789";
    char *p = result;
    if (value < 0) {
        *p++ = '-';
        value *= -1;
    }

    /* move number of required chars and null terminate */
    int shift = value;
    do {
        ++p;
        shift /= 10;
    } while (shift);
    *p = '\0';

    /* populate result in reverse order */
    do {
        *--p = digit [value % 10];
        value /= 10;
    } while (value);

    return result;
}

any base number: (taken from http://www.strudel.org.uk/itoa/)

/* preferred itoa - good for any base */
char *
itoa (int value, char *result, int base)
{
    // check that the base if valid
    if (base < 2 || base > 36) { *result = '\0'; return result; }

    char* ptr = result, *ptr1 = result, tmp_char;
    int tmp_value;

    do {
        tmp_value = value;
        value /= base;
        *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
    } while ( value );

    // Apply negative sign
    if (tmp_value < 0) *ptr++ = '-';
    *ptr-- = '\0';
    while (ptr1 < ptr) {
        tmp_char = *ptr;
        *ptr--= *ptr1;
        *ptr1++ = tmp_char;
    }
    return result;
}

Since it is done with pointers and without reliance on any C function that would require malloc, then I see do reason it wouldn't work in Pebble. However I am not familiar with Pebble.

Lozier answered 9/6, 2014 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.