cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert integer to string?

antonius
Senior
Posted on January 02, 2014 at 11:47

struct tm time;

   char year[50];

Guys,

I want to display year into LCD screen but I got 00 on LCD, any clues on how to do it ?

thanks

 printf(''Time: %d-%d-%d   %02d:%02d:%02d \r\n'', time.tm_year, \

                   time.tm_mon+1, time.tm_mday,\

                   time.tm_hour, time.tm_min, time.tm_sec);

    sprintf(year,''%.2f'',time.tm_year);

 

    lcd_string(year);

#!complicated
31 REPLIES 31
Posted on January 04, 2014 at 15:59

Then you'll need to review the lcd_string() code, but it's clearly not going to accept an integer as a parameter if it is expecting a string.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
trevor23
Associate III
Posted on January 04, 2014 at 16:38

You previously said that ''00'' was appearing on the LCD. Is this for any string you send e.g. if you used LCD_string(''35'') would it display 35 as expected or would it display ''00''?

You need to be more specific and describe exactly what does and what does not work.
antonius
Senior
Posted on January 05, 2014 at 06:24

Yes it was displaying 00 before.....

Here's the time variable I have :

struct tm {

    int tm_sec;   /* seconds after the minute, 0 to 60

                     (0 - 60 allows for the occasional leap second) */

    int tm_min;   /* minutes after the hour, 0 to 59 */

    int tm_hour;  /* hours since midnight, 0 to 23 */

    int tm_mday;  /* day of the month, 1 to 31 */

    int tm_mon;   /* months since January, 0 to 11 */

    int tm_year;  /* years since 1900 */

    int tm_wday;  /* days since Sunday, 0 to 6 */

    int tm_yday;  /* days since January 1, 0 to 365 */

    int tm_isdst; /* Daylight Savings Time flag */

    union {       /* ABI-required extra fields, in a variety of types */

        struct {

            int __extra_1, __extra_2;

        };

        struct {

            long __extra_1_long, __extra_2_long;

        };

        struct {

            char *__extra_1_cptr, *__extra_2_cptr;

        };

        struct {

            void *__extra_1_vptr, *__extra_2_vptr;

        };

    };

};

lcd_string code :

void lcd_string(unsigned char *str){

    

    while(*str){

        lcd_data(*str++);

    }

    

}

antonius
Senior
Posted on January 05, 2014 at 07:22

it displays something already, in unix time :

the code :

sprintf(year,''%2d'',time.tm_year);

lcd_cmd(LCD_CLEAR);

lcd_string(''YEAR: '');

lcd_string(year);

vTaskDelay(15000);

on LCD, now another puzzle for me for converting that value to 2...any clues ? thanks

________________

Attachments :

IMG_20140105_141247.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I19a&d=%2Fa%2F0X0000000bj7%2F7xud0rBkhUeZnRi2iOXedL7qX1qOyN9qS2hdz8_ETEg&asPdf=false
Posted on January 05, 2014 at 07:59

any clues ?

One might reasonably assume the structure simply doesn't contain what you want it too?

time.tm_year

= 1234; // try setting it explicitly

sprintf(year,''%2d'',time.tm_year);

               lcd_cmd(LCD_CLEAR);

               lcd_string(''YEAR: '');

               lcd_string(year);

               vTaskDelay(15000);

I'll observed that printf/scanf functions are stack hogs, and that issues can occur with the locale if they collide. Understand your stack size allocation, and actual usage.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aa_talaat
Associate II
Posted on January 05, 2014 at 18:22

Hi,

This is the routine I am using to display integers on my Nokia 5110 LCD in case it can be of any help. It displays up to 5 digits, and you can control the number of digits to display based on the last parameter passed. So, for a day, the digit_num is 2 and for a year, the digit_num is 4.

/*
* Takes an integer and display it as ascii on the LCD
* state: normal or reverse Video
* leading: how may digits to display from the right side
* if zero that means display the full number without leading zeros
*/
void LCD5110_Write_Dec(uint16_t b, uint8_t state, uint8_t digit_num)
{
unsigned char datas[5],i,j;
datas[0] = b/10000;
b = b - datas[0]*10000;
datas[1] = b/1000;
b = b - datas[1]*1000;
datas[2] = b/100;
b = b - datas[2]*100;
datas[3] = b/10;
b = b - datas[3]*10;
datas[4] = b;
datas[0]+=48;
datas[1]+=48;
datas[2]+=48;
datas[3]+=48;
datas[4]+=48;
/* find the first non zero digit to display - display at least one digit */
for (i = 0; i < 
5
; i++) {
if (datas[i] != 48) {
break;
}
}
if (digit_num > 0 && digit_num < 6){ // valid range
i = 5-digit_num;
}
for (j = i; j < 5; j++) {
if (state == NORMAL) {
LCD5110_write_char(datas[j]);
} else {
LCD5110_write_char_rev(datas[j]);
}
}
}

antonius
Senior
Posted on January 05, 2014 at 23:35

Thanks for the clue, I'll try it....

antonius
Senior
Posted on January 05, 2014 at 23:37

Is the 5110 LCD like this one ?

http://www.ebay.com.au/itm/Model-Durable-84-48-LCD-Module-White-backlight-adapter-pcb-for-Nokia-5110-N4U8-/390629169570?pt=AU_MobilePhoneAccessories&hash=item5af35019a2

If you don't mind, can I have the initialization code, please ?

thank you

antonius
Senior
Posted on January 06, 2014 at 06:30

yea, it's working, displaying 1234......so what do I miss here ?

Here's the variable I have here :

struct tm {

    int tm_sec;   /* seconds after the minute, 0 to 60

                     (0 - 60 allows for the occasional leap second) */

    int tm_min;   /* minutes after the hour, 0 to 59 */

    int tm_hour;  /* hours since midnight, 0 to 23 */

    int tm_mday;  /* day of the month, 1 to 31 */

    int tm_mon;   /* months since January, 0 to 11 */

    int tm_year;  /* years since 1900 */

    int tm_wday;  /* days since Sunday, 0 to 6 */

    int tm_yday;  /* days since January 1, 0 to 365 */

    int tm_isdst; /* Daylight Savings Time flag */

    union {       /* ABI-required extra fields, in a variety of types */

        struct {

            int __extra_1, __extra_2;

        };

        struct {

            long __extra_1_long, __extra_2_long;

        };

        struct {

            char *__extra_1_cptr, *__extra_2_cptr;

        };

        struct {

            void *__extra_1_vptr, *__extra_2_vptr;

        };

    };

};

Posted on January 06, 2014 at 07:21

yea, it's working, displaying 1234......so what do I miss here ?

Whatever is initializing the structure, doesn't seem to be doing it's job.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..