Solved

Transformation from char to float

Posted on 2003-10-29
7
712 Views
Last Modified: 2010-07-27
Hi,
I try to read a number from a char variable. The value of the char is something like "$GPGGA,11111.11,...". I would like to transform the "11111.11" to his float value (11111.11). I am using the BCB6. Is there any command in C++ to do it?

Thanks,
Ronald    
0
Comment
Question by:rsch1973
7 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9641830
There is the function in Delphi.
function StrToFloat(const S: string): Extended;

I think that there is the same function in BCB
0
 
LVL 22

Expert Comment

by:grg99
ID: 9641857
One simple way woul dbe to use strchr() to find the first comma, then strchr() from there+1 to find the nexxt one,
then strncpy to extract the number to another string variable, then sscanf or atof() on that.

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9641872
Sample from MSDN:

/* SSCANF.C: This program uses sscanf to read data items
 * from a string named tokenstring, then displays them.
 */

#include <stdio.h>

void main( void )
{
   char  tokenstring[] = "15 12 14...";
   char  s[81];
   char  c;
   int   i;
   float fp;

   /* Input various data from tokenstring: */
   sscanf( tokenstring, "%s", s );
   sscanf( tokenstring, "%c", &c );
   sscanf( tokenstring, "%d", &i );
   sscanf( tokenstring, "%f", &fp );

   /* Output the data read */
   printf( "String    = %s\n", s );
   printf( "Character = %c\n", c );
   printf( "Integer:  = %d\n", i );
   printf( "Real:     = %f\n", fp );
}

Output
String    = 15
Character = 1
Integer:  = 15
Real:     = 15.000000

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:bcladd
ID: 9642018
In addition to sscanf, atof will convert a pointer to a C-style string (char * at a null-terminated string). You can use grg99's suggestion to find the beginning of the string you want to convert and then call atof:

char * theLine;
// you read the value into theLine here
const char * comma;
comma = strchr(theLine, ',');
++comma;
float value = atof(comma);


Hope this helps, -bcl
0
 
LVL 86

Expert Comment

by:jkr
ID: 9642141
Try

#include <string>
#include <sstream>
#include <iostream>
#include <stdlib.h>

using namespace std;

void main () {

    float f;
    char* pc;
    string s;
    stringstream ss;

    ss << "$GPGGA,11111.11,...";

    getline ( ss, s, ','); // extract 'til 1st ','

    cout << s << endl;

    getline ( ss, s, ','); // extract 'til 2nd ','

    cout << s << endl;

    f = (float) strtod ( s.c_str (), &pc); // convert to 'float'

    cout << f << endl;
}
0
 
LVL 2

Accepted Solution

by:
antoinebf earned 250 total points
ID: 9642176
Hi,
What follows is a nice way of doing it with STL:

#include <sstream>
#include <string>
...
    float fValue(0);
    std::string myString("$GPGGA,11111.11,...");
    std::string::size_type pos1 = myString.find_first_of(',', 0);
    if (pos1 != std::string::npos)
    {
        std::string::size_type pos2 = myString.find_first_of(',', pos1 + 1);
        std::string subString(myString.substr(pos1 + 1, pos2 != std::string::npos ? pos2 - pos1 - 1 : std::string::npos));
        std::istringstream iss(subString);
        iss >> fValue;
    }
...

You can obviously play with the condition to make sure you convert only if possible, or if other conditions are met, but the basic idea here is the use the right shift operator.

hth

A.
0
 
LVL 1

Expert Comment

by:fsign21
ID: 9642189
Or try this:

#include <string>
#include <iostream>
#include <sstream>
#include <stdlib.h>

using namespace std;

void main () {
  double d;
  string val("$GGG-1111.11TZR$");
  size_t pos = val.find_first_of("+-0123456789");

  if(pos==string::npos) {//not found
    d = 0.0;
  } else {
//1.Solution => use atof
  d= atof(val.c_str()+pos);

//2.Solution => use stringstream
  stringstream sio;
  sio << val.substr(pos);
  sio >> d;
  }
  cout << "Double: " << d << endl;
}
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now