Skip to main content
antonius
Associate III
January 2, 2014
Question

How to convert integer to string?

  • January 2, 2014
  • 31 replies
  • 8702 views
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
    This topic has been closed for replies.

    31 replies

    chen
    Associate II
    January 2, 2014
    Posted on January 02, 2014 at 12:10

    Hi

    The 'printf' can depend on the library that you are using, which can depend on the tool chain that  you are using.

    IAR tool chain does not support the '%f' in the 'basic' library - you have to use the 'full' library but that then adds 20K of binary (IAR provide the implementations of the STD libraries - you choose which one to use basic, full + another which I forget now).

    So what tool chain are you using?

    antonius
    antoniusAuthor
    Associate III
    January 2, 2014
    Posted on January 02, 2014 at 12:15

    I'm using MDK-ARM, isn't it gcc ?

    antonius
    antoniusAuthor
    Associate III
    January 2, 2014
    Posted on January 02, 2014 at 12:21

    I got this warning :

    ..\USER\RTC\RTC_Time.c(481): warning:  #167-D: argument of type ''char *'' is incompatible with parameter of type ''unsigned char *''

    code :

     struct tm time;

       char year[50];

       time = Time_GetCalendarTime();

       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,''%02d'',time.tm_year);

        lcd_string(year);

    antonius
    antoniusAuthor
    Associate III
    January 2, 2014
    Posted on January 02, 2014 at 12:23

    it's running but freezed in the middle.....any ideas ?

    chen
    Associate II
    January 2, 2014
    Posted on January 02, 2014 at 12:30

    Hi

    Where is the printf going to ?

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

    will probably not be null terminated - so when ''lcd_string(year);''

    executes - it probably goes on beyond the buffer you allocated and crashed.

    chen
    Associate II
    January 2, 2014
    Posted on January 02, 2014 at 13:14

    Hi

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

    will probably not be null terminated''

    Forget that - apparently it does Null terminate.

    trevor23
    Associate III
    January 2, 2014
    Posted on January 02, 2014 at 13:45

    At least 2 problems here:

    1. Should be ''%02d'' not ''%.2f''

    2. tm_year is an integer containing number of years since 1900. Therefore to get year as 2 digits would be something like this ''sprintf(year, ''%02d'', (time.tm_year + 1900) % 100);''
    Tesla DeLorean
    Guru
    January 2, 2014
    Posted on January 02, 2014 at 14:20

    2. tm_year is an integer containing number of years since 1900. Therefore to get year as 2 digits would be something like this ''sprintf(year, ''%02d'', (time.tm_year + 1900) % 100);''

    Same two digits without the addition, mind.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    trevor23
    Associate III
    January 2, 2014
    Posted on January 02, 2014 at 16:21

    Same two digits without the addition, mind.

    Indeed Clive
    Andrew Neil
    Super User
    January 2, 2014
    Posted on January 02, 2014 at 23:40

    ''I'm using MDK-ARM, isn't it gcc ?''

    Most definitely

    not

    !

    Keil (an ARM company) makes & sells tools on a commercial, closed-source basis.

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.