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
chen
Associate II
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
Senior
Posted on January 02, 2014 at 12:15

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

antonius
Senior
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
Senior
Posted on January 02, 2014 at 12:23

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

chen
Associate II
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
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
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);''
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
Up vote any posts that you find helpful, it shows what's working..
trevor23
Associate III
Posted on January 02, 2014 at 16:21

Same two digits without the addition, mind.

Indeed Clive