New to iPhone
New to iPhone

Reputation: 215

Float to string

There has got to be an easier way to do this. What I am trying to do is cast a group of int and floats to a char then combine them into a single char. It is not building the string str as anticipated.
Written in C.

char *str_W1;
char *str_W2;
char *str_W3;
char *str_W4;
char *str_W5;
char *str_W6;
char *str_VBAT;
char *str_day;
char *str_month;
char *str_year;
char *str_hour;
char *str_minute;

sprintf(str_day, "%d", time.day);       //casting int to string
sprintf(str_month, "%d", time.month);   //casting int to string
sprintf(str_year, "%d", time.year);     //casting int to string
sprintf(str_hour, "%d", time.hour);     //casting int to string
sprintf(str_minute, "%d", time.minute); //casting int to string
sprintf(str_W1, "%.2f", value_w1);      //casting float to string
sprintf(str_W2, "%.2f", value_w2);      //casting float to string
sprintf(str_W3, "%.2f", value_w3);      //casting float to string
sprintf(str_W4, "%.2f", value_w4);      //casting float to string
sprintf(str_W5, "%.2f", value_w5);      //casting float to string
sprintf(str_W6, "%.2f", value_w6);      //casting float to string 
sprintf(str_VBAT, "%.2f", value_vbat);  //casting float to string

char *str;
/* Building One String */
strcpy (str, str_day );
strcat (str, "/");
strcat (str, str_month);
strcat (str, "/");
strcat (str, str_year);
strcat (str, ",");
strcat (str, str_hour);
strcat (str, ":");
strcat (str, str_minute);
strcat (str, ",");
strcat (str, str_W1);
strcat (str, ",");
strcat (str, str_W2);
strcat (str, ",");
strcat (str, str_W3);
strcat (str, ",");
strcat (str, str_W4);
strcat (str, ",");
strcat (str, str_W5);
strcat (str, ",");
strcat (str, str_W6);
strcat (str, ",");
strcat (str, str_VBAT);

Upvotes: 0

Views: 2497

Answers (3)

Nicholas Carey
Nicholas Carey

Reputation: 74267

You really should work through Kernighan & Ritchies The C Programming Language (2nd ed.).

But in answer to your question you use strftime() from time.h and sprintf() from stdio.h.

Since it looks like your using a non-standard time structure, you'll want a mapping functon, something like this:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

typedef struct moment_in_time
{
  int year   ; // year   (e.g. 2014)
  int month  ; // month  (1-12)
  int day    ; // day    (1-31)
  int hour   ; // hour   (0-23)
  int minute ; // minute (0-59)
} MOMENT_IN_TIME ;


void convert_moment_to_struct_tm( MOMENT_IN_TIME *m , struct tm *p )
{
  struct tm  src  = { 0      , m->minute    , m->hour        ,
                      m->day , m->month - 1 , m->year - 1900 ,
                      0  , 0 , // day-of-week and day-of-year are ignored by mktime()
                      -1 ,      // and we've got no idea if Daylight Savings (Summer) Time is in effect or not
                    } ;
  time_t     tx = mktime(&src) ;
  struct tm *tm = localtime( &tx ) ;

  memcpy( p , tm , sizeof(struct tm) ) ;

  return ;
}

Once you've got a straight struct tm, you should be able to write something like this:

char *format_time( struct tm *tm , double w1, double w2, double w3, double w4, double w5, double w6, double vbat)
{
  size_t     bufl = 8192 * sizeof(char) ;
  char      *buf  = calloc(8192, sizeof(char)) ;
  char      *p    = buf ;

  p += strftime( p , bufl , "%d/%m/%Y, %H:%M, " , tm ) ;
  p += sprintf(  p , "%.2f, %.2f, %.2f, %.2f, %.2f, %.2f" ,
         w1 , w2 , w3 , w4 , w5 , w6
         ) ;
  p += sprintf(p, ", %.2f" , vbat ) ;

  return buf;
}

Then you can say something like this:

int main(  int argc , char *argv[] )
{
  MOMENT_IN_TIME moment = { 2014 , 05 , 26 , 15 , 55 , } ;
  struct tm      tm     ;

  convert_moment_to_struct_tm( &moment , &tm ) ;

  char *formatted = format_time( &tm , 1.234 , 2.345 , 3.456 , 4.567 , 5.678 , 6.789 , 7.890 ) ;

  fwrite( formatted , sizeof(*formatted) , strlen(formatted) , stdout );
  free(formatted);

  return 0;
}

And produce

26/05/2014, 15:55, 1.23, 2.35, 3.46, 4.57, 5.68, 6.79, 7.89

Upvotes: 2

user4815162342
user4815162342

Reputation: 154916

You can use sprintf (or better snprintf) with more than a single format character. This removes the need for strcpy and strcat. Also, don't forget to actually allocate an array for the string. If you are building a small string, you can preallocate an array of fixed size and populate it with code like this:

char s[256];
snprintf(s, sizeof s, "%d/%d/%d,%d:%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f",
         time.day, time.month, time.year, time.hour, time.minute,
         value_w1, value_w2, value_w3, value_w4, value_w5, value_w6,
         value_vbat);

// proceed working with s

Upvotes: 2

CS Pei
CS Pei

Reputation: 11047

First you need to allocate memory for your pointers.

Second, as Kerreck SB pointed out, you may want to look at strftime, like this

    //Format time
    strftime(time,10,"%T",localtime(&curtime));

Upvotes: 2

Related Questions