LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2004, 06:11 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Rep: Reputation: 47
C++ and C# Parse a string into a numerical type


I need this question answered for both c# and c++, an answer for one or the other is nice, but both are needed.

is there a builtin function that can take a string and parse it into a double/float/int?

for instance if I have a String vaiable called string1, and it contains "1234.567" could I do something like:

double double1 = ParseStringToDouble(string1);

I need this for both C# (CSharp) and C++
 
Old 02-22-2004, 06:32 PM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
I can answer this question with C++.

Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string str = "1.222"; 
double d;			
const char *c_ptr=str.c_str();  // method c_str() returns a read-only C-style Null terminated string.

d=atof(c_ptr);				//atof() converts a C string to a double 
cout << d;                              // EDIT WAS HERE				
return 0;
}
EDIT{ originally had cout << c_ptr; should have been cout <<d; }

Last edited by jinksys; 02-22-2004 at 07:17 PM.
 
Old 02-22-2004, 06:53 PM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
Thank you, I actually found a list of convert functions for C++, it sais atof converts to a float, that for a double you would do something like this:

d = strtod("1234.567");
however, it does not say anything for a string stored ina variable, would I need to do your:
const char *c_ptr=str.c_str();
and then:
d = strtod(c_ptr); ?
or would this work as is:
d = strtod(str);

and last question for you:
what does "cout << c_ptr;" do? does it simply print to see if anything was left over? or does it print what was placed into "d"? or does it do somethign else entirely? (I am a c++ student, halfway through first of the 3 classes my cc offers, I am also taking java, C#, and bash scripting simeltaniously)


Thank you Very much, anyone else out there have the answer for C#?
 
Old 02-22-2004, 06:57 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
For C#, you would use
double d = Convert.ToDouble("1234.56");
 
Old 02-22-2004, 07:00 PM   #5
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
deiussum:

would I need to add a new using statement to the top of the file?
at the moment I have:

using System;
using System.Collections;

is anything else needed for the conversion?
 
Old 02-22-2004, 07:18 PM   #6
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Quote:
Originally posted by exodist
Thank you, I actually found a list of convert functions for C++, it sais atof converts to a float, that for a double you would do something like this:

d = strtod("1234.567");
however, it does not say anything for a string stored ina variable, would I need to do your:
const char *c_ptr=str.c_str();
and then:
d = strtod(c_ptr); ?
or would this work as is:
d = strtod(str);

and last question for you:
what does "cout << c_ptr;" do? does it simply print to see if anything was left over? or does it print what was placed into "d"? or does it do somethign else entirely? (I am a c++ student, halfway through first of the 3 classes my cc offers, I am also taking java, C#, and bash scripting simeltaniously)


Thank you Very much, anyone else out there have the answer for C#?
atof() returns a double, atoi() returns an int, and atol() returns a long. and yes, if you are
using C++'s string class you will need to convert it to a C-style string for either method to work.
for your last question, (and make sure you notice my edit) cout <<d; prints variable d (to show that d was actually given the numeric value of str).

Last edited by jinksys; 02-22-2004 at 07:19 PM.
 
Old 02-22-2004, 09:23 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by exodist
deiussum:

would I need to add a new using statement to the top of the file?
at the moment I have:

using System;
using System.Collections;

is anything else needed for the conversion?
Nope, the Convert class is in the System namespace, so nothing else should be needed.

BTW, this is in the .Net library from MS. If you are using something like DotGNU, it's possible that it is one of the things that hasn't been implemented yet. I know the last time I took a look at DotGNU it still needed work done on a number of the classes, anyway... An alternative method is the Double.Parse method, which essentially does the same thing as Convert.ToDouble with the exception that Convert.ToDouble takes more data types than just strings...


Last edited by deiussum; 02-22-2004 at 09:29 PM.
 
Old 02-22-2004, 09:38 PM   #8
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
Thank you, I am aware of the limitations, I also have visual studio .net on a windows box I just braught up, I get it free through msdn because I am a student, I use it to test.
 
Old 02-23-2004, 06:15 AM   #9
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Alternatively, in c++

Code:
#include <sstream>

template<typename T>
void string_to_type( const std::string& s, T& t )
{
    std::istringstream iss( s );
    iss >> t;                                    // check this if error handling is required
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
c++ parse date string?? blizunt7 Programming 25 05-18-2006 03:12 PM
Parse --static string no delimiters DaFrEQ Programming 2 04-08-2005 02:06 PM
Parse RPM version string in Bash jimwelc Linux - Newbie 1 02-28-2005 05:22 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration