rsch1973
asked on
Transformation from char to float
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
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
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.
then strncpy to extract the number to another string variable, then sscanf or atof() on that.
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
/* 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
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
char * theLine;
// you read the value into theLine here
const char * comma;
comma = strchr(theLine, ',');
++comma;
float value = atof(comma);
Hope this helps, -bcl
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;
}
#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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("+-01234 56789");
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;
}
#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("+-01234
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;
}
function StrToFloat(const S: string): Extended;
I think that there is the same function in BCB