CVI Double to Char

706 Views Asked by At

I am trying to concatenate strings with numeric (double) values and texts.

My current code:

char nameLed[256];      //Nom de la led
char colorLed[256];     //La couleur de la led
char I_directLed[100];      //L'intensité direct que peut supporter la led
double U_directLed;     //La tension direct que peut supporter la led
char commentLed[256];   //Le commentaire sur la led
char chaineSaveLed[1000];//Chaine concaténé

GetCtrlVal(panel, TABPANEL_1_ST_Name, nameLed);             //Panel > Tab Champ
GetCtrlVal(panel, TABPANEL_1_ST_Color, colorLed);           //Panel > Tab Champ
GetCtrlVal(panel, TABPANEL_1_I_Direct_Led, &I_directLed);   //Panel > Tab Champ
GetCtrlVal(panel, TABPANEL_1_U_Led_Direct, &U_directLed);   //Panel > Tab Champ
GetCtrlVal(panel, TABPANEL_1_TXT_Comment, commentLed);      //Panel > Tab Champ

//Créer la chaine à enregistrer au format CSV
//Concaténation de chaine
strcat (chaineSaveLed, nameLed);
strcat (chaineSaveLed, ",");
strcat (chaineSaveLed, colorLed);
strcat (chaineSaveLed, ",");
strcat (chaineSaveLed, I_directLed);
strcat (chaineSaveLed, ",");
strcat (chaineSaveLed, U_directLed);
strcat (chaineSaveLed, ",");
strcat (chaineSaveLed, commentLed);
strcat (chaineSaveLed, "/n");

printf ("%s\n", chaineSaveLed);

My problem is on "I_directLed" and "U_directLed".

I have to convert the double to char.

Thanks for your help.

1

There are 1 best solutions below

2
On

If I understand what you mean correctly, the best way to do this is using sprintf(). It allows you to write to a string and format it, similar to how printf() works. So for example, you could replace your code with:

//Define variables
char nameLed[256];   
char colorLed[256];    
char I_directLed[100];  
double U_directLed;   
char commentLed[256];  
char chaineSaveLed[1000];

//Get value from GUI
GetCtrlVal(panel, TABPANEL_1_ST_Name, nameLed);             
GetCtrlVal(panel, TABPANEL_1_ST_Color, colorLed);          
GetCtrlVal(panel, TABPANEL_1_I_Direct_Led, &I_directLed);  
GetCtrlVal(panel, TABPANEL_1_U_Led_Direct, &U_directLed);   
GetCtrlVal(panel, TABPANEL_1_TXT_Comment, commentLed);    

//Concatenates all of the given variables into the string chainSaveLed
sprintf(chaineSaveLed,"%s,%s,%s,%lf,%s\n",nameLed, colorLed, I_directLed, U_directLed, commentLed);

//Print output
printf ("%s\n", chaineSaveLed);