Advertisement

04.23.2003 at 06:30AM PDT, ID: 20593646
[x]
Attachment Details

Convert double value to char *

Asked by sroz in C Programming Language

Tags: double, convert, char

hello,

is there a quick solution to convert a double value to char* ?
my dll takes a double array from a vba interface and i must use a method which takes these values as a char * array. as i manage only the middle i must find a solution to do the convertion.

i've some code but i'm afraid it to be long and when i go out from DoubletoChar lnRes is null.

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

void main( void )
{
     double lnDouble = -3.1415926535;
     char * lsRes = NULL;
     int lnRep = 0;

     lnRep = DoubleToChar(lnDouble, lsRes);
     printf("resultat : %s\n", lsRes);
}

int DoubleToChar(double pnDouble, char *psBuffer) {
     int lnDecimal = 0;
     int lnSign = 0;
     int lnI = 0;

     psBuffer = _fcvt(pnDouble, 15, &lnDecimal, &lnSign );

     for (lnI = strlen(psBuffer); lnI >= lnDecimal; lnI--) {
          psBuffer[lnI + 1] = psBuffer[lnI];
          if (lnI == lnDecimal)
               psBuffer[lnI] = '.';
     }

     if (lnSign == 1) {
          for (lnI = strlen(psBuffer); lnI >= 0; lnI--) {
               psBuffer[lnI + 1] = psBuffer[lnI];
               if (lnI == 0)
                    psBuffer[lnI] = '-';
          }
     }

     return 1;
}

what can i do to have the good value in lnRes and to go faster(i may have to convert an array of 1000 values one by one)Start Free Trial
 
Loading Advertisement...
 
[+][-]04.23.2003 at 06:44AM PDT, ID: 8380059

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.23.2003 at 06:45AM PDT, ID: 8380069

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.23.2003 at 07:00AM PDT, ID: 8380205

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.24.2003 at 06:18AM PDT, ID: 8388307

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: double, convert, char
Sign Up Now!
Solution Provided By: DaveHumphrey
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32